home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Contributed / SpriteWorld / SpriteWorld Files / Sources / SpriteWorld.c < prev    next >
Encoding:
Text File  |  2000-10-06  |  80.0 KB  |  2,706 lines  |  [TEXT/CWIE]

  1. ///--------------------------------------------------------------------------------------
  2. //    SpriteWorld.c
  3. //
  4. //    Portions are copyright: © 1991-94 Tony Myles, All rights reserved worldwide.
  5. //
  6. //    Description:    implementation of the sprite world architecture
  7. ///--------------------------------------------------------------------------------------
  8.  
  9. #ifndef __QUICKDRAW__
  10. #include <QuickDraw.h>
  11. #endif
  12.  
  13. #ifndef __DEVICES__
  14. #include <Devices.h>
  15. #endif
  16.  
  17. #ifndef __RETRACE__
  18. #include <Retrace.h>
  19. #endif
  20.  
  21. #ifndef __MEMORY__
  22. #include <Memory.h>
  23. #endif
  24.  
  25. #ifndef __GESTALT__
  26. #include <Gestalt.h>
  27. #endif
  28.  
  29. #ifndef __TIMER__
  30. #include <Timer.h>
  31. #endif
  32.  
  33. #ifndef __SWCOMMON__
  34. #include <SWCommonHeaders.h>
  35. #endif
  36.  
  37. #ifndef __SPRITEWORLDUTILS__
  38. #include <SpriteWorldUtils.h>
  39. #endif
  40.  
  41. #ifndef __SPRITEWORLD__
  42. #include <SpriteWorld.h>
  43. #endif
  44.  
  45. #ifndef __SPRITELAYER__
  46. #include <SpriteLayer.h>
  47. #endif
  48.  
  49. #ifndef __SPRITE__
  50. #include <Sprite.h>
  51. #endif
  52.  
  53. #ifndef __SPRITEFRAME__
  54. #include <SpriteFrame.h>
  55. #endif
  56.  
  57. #ifndef __BLITPIXIEINTERFACE__
  58. #include <BlitPixieInterface.h>
  59. #endif
  60.  
  61. #ifndef __SCROLLING__
  62. #include <Scrolling.h>
  63. #endif
  64.  
  65. #ifndef __TILING__
  66. #include <Tiling.h>
  67. #endif
  68.  
  69. #include <BlitPixieHeader.h>    // for BlitPixieGetProcessorType
  70.  
  71. RgnHandle             gSWCollisionSectRgn = NULL;            //  Initialized by SWEnterSpriteWorld
  72. RgnHandle             gSWCollisionSpareRgn = NULL;            //    (used by SWRegionCollision )
  73.  
  74. SpriteWorldPtr        gSWCurrentSpriteWorld = NULL;
  75. SpritePtr            gSWCurrentSpriteBeingDrawn = NULL;
  76.  
  77. SpriteWorldPtr        gSWCleanUpSpriteWorldP = NULL;
  78. CleanUpCallBackPtr    gSWCleanUpCallBackP = NULL;
  79.  
  80.  
  81. ///--------------------------------------------------------------------------------------
  82. //    SWEnterSpriteWorld
  83. ///--------------------------------------------------------------------------------------
  84.  
  85. SW_FUNC OSErr SWEnterSpriteWorld(void)
  86. {
  87.     OSErr    err = noErr;
  88.  
  89.     gSWmmuMode = GetMMUMode();
  90.     
  91.     if ( !SWHasSystem7() )
  92.     {
  93.         err = kSystemTooOldErr;
  94.     }
  95.     
  96.     BlitPixieGetProcessorType();
  97.     
  98.     if (gSWCollisionSectRgn == NULL && gSWCollisionSpareRgn == NULL)
  99.     {
  100.         gSWCollisionSectRgn = NewRgn();
  101.         gSWCollisionSpareRgn = NewRgn();
  102.     }
  103.     else
  104.     {
  105.         err = kAlreadyCalledErr;
  106.     }
  107.     
  108.     SWSetStickyIfError( err );
  109.     return err;
  110. }
  111.  
  112.  
  113. ///--------------------------------------------------------------------------------------
  114. //    SWCreateSpriteWorld
  115. ///--------------------------------------------------------------------------------------
  116.  
  117. SW_FUNC OSErr SWCreateSpriteWorld(
  118.     GDHandle            mainGDH,
  119.     SpriteWorldPtr        *spriteWorldP,
  120.     FramePtr            windowFrameP,
  121.     FramePtr            backFrameP,
  122.     FramePtr            workFrameP,
  123.     short                maxDepth)
  124. {
  125.     OSErr            err;
  126.     SpriteWorldPtr    tempSpriteWorldP;
  127.     PixMapHandle    basePixMap;
  128.     
  129.  
  130.     err = noErr;
  131.     *spriteWorldP = NULL;
  132.  
  133.     tempSpriteWorldP = (SpriteWorldPtr)NewPtrClear((Size)sizeof(SpriteWorldRec));
  134.     if (tempSpriteWorldP == NULL)
  135.         err = MemError();
  136.     
  137.     if (err == noErr)
  138.     {
  139.             // Create the deadSpriteLayerP, but don't add it to the linked layer list
  140.         err = SWCreateSpriteLayer(&tempSpriteWorldP->deadSpriteLayerP);
  141.         if (err != noErr)
  142.             DisposePtr((Ptr)tempSpriteWorldP);
  143.     }
  144.  
  145.     if (err == noErr)
  146.     {
  147.         basePixMap = (**mainGDH).gdPMap;
  148.         if ( maxDepth == 0 || (**basePixMap).pixelSize <= maxDepth )
  149.         {
  150.             tempSpriteWorldP->pixelDepth = (**basePixMap).pixelSize;
  151.         }
  152.         else
  153.         {
  154.             tempSpriteWorldP->pixelDepth = maxDepth;
  155.         }
  156.         
  157.         tempSpriteWorldP->headSpriteLayerP = NULL;
  158.         tempSpriteWorldP->tailSpriteLayerP = NULL;
  159.         
  160.         tempSpriteWorldP->mainSWGDH = mainGDH;
  161.         tempSpriteWorldP->windowFrameP = windowFrameP;
  162.         tempSpriteWorldP->extraBackFrameP = NULL;
  163.         tempSpriteWorldP->backFrameP = backFrameP;
  164.         tempSpriteWorldP->workFrameP = workFrameP;
  165.         tempSpriteWorldP->offscreenDrawProc = SWStdWorldDrawProc;
  166.         tempSpriteWorldP->screenDrawProc = SWStdWorldDrawProc;
  167.         tempSpriteWorldP->doubleRectDrawProc = NULL;
  168.         tempSpriteWorldP->postEraseCallBack = NULL;
  169.         tempSpriteWorldP->postDrawCallBack = NULL;
  170.         tempSpriteWorldP->windRect = windowFrameP->frameRect;
  171.         tempSpriteWorldP->backRect = backFrameP->frameRect;
  172.         tempSpriteWorldP->originalWindRect = windowFrameP->frameRect;
  173.         tempSpriteWorldP->originalBackRect = backFrameP->frameRect;
  174.         
  175.         tempSpriteWorldP->headUpdateRectP = NULL;
  176.         
  177.         tempSpriteWorldP->scrollRectMoveBounds.left = 0;
  178.         tempSpriteWorldP->scrollRectMoveBounds.top = 0;
  179.         tempSpriteWorldP->scrollRectMoveBounds.right = 32767;
  180.         tempSpriteWorldP->scrollRectMoveBounds.bottom = 32767;
  181.         tempSpriteWorldP->horizScrollRectOffset = 0;
  182.         tempSpriteWorldP->vertScrollRectOffset = 0;
  183.         tempSpriteWorldP->horizScrollDelta = 0;
  184.         tempSpriteWorldP->vertScrollDelta = 0;
  185.         tempSpriteWorldP->worldMoveProc = NULL;
  186.         tempSpriteWorldP->thereAreNonScrollingLayers = false;
  187.         
  188.             // Make sure visScrollRect is at 0,0 top-left corner
  189.         tempSpriteWorldP->visScrollRect = windowFrameP->frameRect;
  190.         OffsetRect(&tempSpriteWorldP->visScrollRect,
  191.             -tempSpriteWorldP->visScrollRect.left, 
  192.             -tempSpriteWorldP->visScrollRect.top);
  193.         tempSpriteWorldP->offscreenScrollRect = tempSpriteWorldP->visScrollRect;
  194.         tempSpriteWorldP->oldVisScrollRect = tempSpriteWorldP->visScrollRect;
  195.         
  196.         tempSpriteWorldP->tileLayerArray = NULL;
  197.         tempSpriteWorldP->tilingCache = NULL;
  198.         tempSpriteWorldP->changedTiles = NULL;
  199.         tempSpriteWorldP->tilingIsInitialized = false;
  200.         tempSpriteWorldP->tilingIsOn = false;
  201.         tempSpriteWorldP->numTilesChanged = 0;
  202.         tempSpriteWorldP->tileChangeProc = NULL;
  203.         tempSpriteWorldP->tileRectDrawProc = SWDrawTilesInRect;
  204.         tempSpriteWorldP->tileMaskDrawProc = SWStdSpriteDrawProc;
  205.         tempSpriteWorldP->partialMaskDrawProc = SWStdSpriteDrawProc;
  206.         
  207.         tempSpriteWorldP->frameHasOccurred = false;
  208.         tempSpriteWorldP->fpsTimeInterval = 0L;
  209.         
  210.         tempSpriteWorldP->lastMicroseconds.hi = 0L;
  211.         tempSpriteWorldP->lastMicroseconds.lo = 0L;
  212.         tempSpriteWorldP->runningTimeCount = 0L;
  213.         
  214.         tempSpriteWorldP->vblTaskRec.hasVBLFired = true;
  215.         tempSpriteWorldP->usingVBL = false;
  216.  
  217.         tempSpriteWorldP->backgroundColor.red = 0;
  218.         tempSpriteWorldP->backgroundColor.green = 0;
  219.         tempSpriteWorldP->backgroundColor.blue = 0;
  220.         tempSpriteWorldP->backgroundValue = 0;
  221.         
  222.         SWClearStickyError();
  223.  
  224.         *spriteWorldP = tempSpriteWorldP;
  225.     }
  226.  
  227.     return err;
  228. }
  229.  
  230. ///--------------------------------------------------------------------------------------
  231. //    SWCreateSpriteWorldFromWindow
  232. ///--------------------------------------------------------------------------------------
  233.  
  234. SW_FUNC OSErr SWCreateSpriteWorldFromWindow(
  235.     SpriteWorldPtr*    spriteWorldP,
  236.     CWindowPtr        srcWindowP,
  237.     Rect*            worldRectP,
  238.     Rect*            offscreenRectP,
  239.     short            maxDepth)
  240. {
  241.     OSErr             err = noErr;
  242.     FramePtr         windowFrameP, backFrameP, workFrameP;
  243.     Rect             tempRect, globalRect, windRect;
  244.     GDHandle        mainGDH;
  245.     PixMapHandle    basePixMap;
  246.     short            depth;
  247.     
  248.     
  249.     *spriteWorldP = NULL;
  250.     windowFrameP = backFrameP = workFrameP = NULL;
  251.     
  252.     SetPort( (WindowPtr)srcWindowP );
  253.     
  254.     windRect = (worldRectP == NULL) ? srcWindowP->portRect : *worldRectP;
  255.     
  256.     globalRect = windRect;
  257.     LocalToGlobal((Point *)&globalRect.top);
  258.     LocalToGlobal((Point *)&globalRect.bottom);
  259.     
  260.     
  261.     mainGDH = GetMaxDevice( &globalRect );
  262.     SetGDevice( mainGDH );
  263.     basePixMap = (**mainGDH).gdPMap;
  264.     if ( maxDepth == 0 || (**basePixMap).pixelSize <= maxDepth )
  265.     {
  266.         depth = (**basePixMap).pixelSize;
  267.     }
  268.     else
  269.     {
  270.         depth = maxDepth;
  271.     }
  272.     
  273.     if ( srcWindowP->portVersion < (short)0xC000 )
  274.     {
  275.         err = kNotCWindowErr;
  276.     }
  277.     
  278.         // If a custom offscreenRect has been requested, use it instead.
  279.     if (offscreenRectP == NULL)
  280.         tempRect = windRect;
  281.     else
  282.         tempRect = *offscreenRectP;
  283.     
  284.         // offset Rect to (0,0) for back and work frames
  285.     OffsetRect(&tempRect, -tempRect.left, -tempRect.top);
  286.     
  287.     if (err == noErr)
  288.     {
  289.             // create window frame
  290.         err = SWCreateWindowFrame(srcWindowP, &windowFrameP, &windRect, tempRect.bottom);
  291.     }
  292.     
  293.     if (err == noErr)
  294.     {
  295.             // create back drop frame
  296.         err = SWCreateFrame(mainGDH, &backFrameP, &tempRect, depth, kCreateGWorld);
  297.     }
  298.  
  299.     if (err == noErr)
  300.     {
  301.             // create work frame
  302.         err = SWCreateFrame(mainGDH, &workFrameP, &tempRect, depth, kCreateGWorld);
  303.     }
  304.  
  305.  
  306.     if (err == noErr)
  307.     {
  308.             // create sprite world
  309.         err = SWCreateSpriteWorld(mainGDH, spriteWorldP, windowFrameP, 
  310.                 backFrameP, workFrameP, maxDepth);
  311.     }
  312.  
  313.     if (err != noErr)
  314.     {
  315.             // an error occurred so dispose of anything we managed to create
  316.  
  317.         if (windowFrameP != NULL)
  318.         {
  319.             SWDisposeWindowFrame(&windowFrameP);
  320.         }
  321.  
  322.         if (backFrameP != NULL)
  323.         {
  324.             SWDisposeFrame(&backFrameP);
  325.         }
  326.  
  327.         if (workFrameP != NULL)
  328.         {
  329.             SWDisposeFrame(&workFrameP);
  330.         }
  331.     }
  332.     
  333.     SWSetStickyIfError( err );
  334.     return err;
  335. }
  336.  
  337.  
  338. ///--------------------------------------------------------------------------------------
  339. //    SWDisposeSpriteWorld
  340. ///--------------------------------------------------------------------------------------
  341.  
  342. SW_FUNC void SWDisposeSpriteWorld(
  343.     SpriteWorldPtr    *spriteWorldPP)
  344. {
  345.     SpriteWorldPtr    spriteWorldP = *spriteWorldPP;
  346.     SpriteLayerPtr     curLayerP;
  347.     SpritePtr         curSpriteP;
  348.     
  349.     
  350.     if (spriteWorldP != NULL)
  351.     {
  352.         SWUnlockSpriteWorld(spriteWorldP);
  353.             
  354.         while ((curLayerP = SWGetNextSpriteLayer(spriteWorldP, NULL)) != NULL)
  355.         {        
  356.                 // dispose of each sprite in the layer
  357.             while ((curSpriteP = SWGetNextSprite(curLayerP, NULL)) != NULL)
  358.             {
  359.                 SWRemoveSprite(curSpriteP);
  360.                 SWDisposeSprite(&curSpriteP);
  361.             }
  362.             
  363.                 // dispose of each layer in the spriteWorld
  364.             SWRemoveSpriteLayer(spriteWorldP, curLayerP);
  365.             SWDisposeSpriteLayer(&curLayerP);
  366.         }
  367.         
  368.         SWDisposeAllSpritesInLayer(spriteWorldP->deadSpriteLayerP);
  369.         SWDisposeSpriteLayer(&spriteWorldP->deadSpriteLayerP);
  370.         
  371.         SWDisposeFrame(&spriteWorldP->backFrameP);
  372.         SWDisposeFrame(&spriteWorldP->workFrameP);
  373.         SWDisposeFrame(&spriteWorldP->extraBackFrameP);
  374.  
  375.         SWDisposeWindowFrame(&spriteWorldP->windowFrameP);
  376.         
  377.         SWExitTiling(spriteWorldP);
  378.         SWSyncSpriteWorldToVBL(spriteWorldP, false);
  379.         
  380.         DisposePtr((Ptr)spriteWorldP);
  381.         *spriteWorldPP = NULL;    // Set original pointer to NULL
  382.     }
  383. }
  384.  
  385.  
  386. ///--------------------------------------------------------------------------------------
  387. //    SWExitSpriteWorld
  388. ///--------------------------------------------------------------------------------------
  389.  
  390. SW_FUNC void SWExitSpriteWorld(void)
  391. {
  392.     if (gSWCollisionSectRgn != NULL)
  393.         DisposeRgn(gSWCollisionSectRgn);
  394.     if (gSWCollisionSpareRgn != NULL)
  395.         DisposeRgn(gSWCollisionSpareRgn);
  396.     
  397.     gSWCollisionSectRgn = NULL;
  398.     gSWCollisionSpareRgn = NULL;
  399. }
  400.  
  401.  
  402. ///--------------------------------------------------------------------------------------
  403. //    SWAddSpriteLayer
  404. ///--------------------------------------------------------------------------------------
  405.  
  406. SW_FUNC void SWAddSpriteLayer(
  407.     SpriteWorldPtr        spriteWorldP,
  408.     SpriteLayerPtr        newSpriteLayerP)
  409. {
  410.     SpriteLayerPtr     tailSpriteLayerP = spriteWorldP->tailSpriteLayerP;
  411.     
  412.     SW_ASSERT(spriteWorldP != NULL);
  413.  
  414.     if (tailSpriteLayerP != NULL)
  415.         tailSpriteLayerP->nextSpriteLayerP = newSpriteLayerP;
  416.     else
  417.         spriteWorldP->headSpriteLayerP = newSpriteLayerP;
  418.     
  419.     newSpriteLayerP->prevSpriteLayerP = tailSpriteLayerP;
  420.     newSpriteLayerP->nextSpriteLayerP = NULL;
  421.  
  422.         // make the new layer the tail
  423.     spriteWorldP->tailSpriteLayerP = newSpriteLayerP;
  424. }
  425.  
  426.  
  427. ///--------------------------------------------------------------------------------------
  428. //    SWRemoveSpriteLayer
  429. ///--------------------------------------------------------------------------------------
  430.  
  431. SW_FUNC void SWRemoveSpriteLayer(
  432.     SpriteWorldPtr    spriteWorldP,
  433.     SpriteLayerPtr    oldSpriteLayerP)
  434. {    
  435.     SW_ASSERT(spriteWorldP != NULL);
  436.     
  437.         // is there a next layer?
  438.     if (oldSpriteLayerP->nextSpriteLayerP != NULL)
  439.     {
  440.             // link the next layer to the prev layer
  441.         oldSpriteLayerP->nextSpriteLayerP->prevSpriteLayerP = oldSpriteLayerP->prevSpriteLayerP;
  442.     }
  443.     else
  444.     {
  445.             // make the prev layer the tail
  446.         spriteWorldP->tailSpriteLayerP = oldSpriteLayerP->prevSpriteLayerP;
  447.     }
  448.  
  449.         // is there a prev layer?
  450.     if (oldSpriteLayerP->prevSpriteLayerP != NULL)
  451.     {
  452.             // link the prev layer to the next layer
  453.         oldSpriteLayerP->prevSpriteLayerP->nextSpriteLayerP = oldSpriteLayerP->nextSpriteLayerP;
  454.     }
  455.     else
  456.     {
  457.             // make the next layer the head
  458.         spriteWorldP->headSpriteLayerP = oldSpriteLayerP->nextSpriteLayerP;
  459.     }
  460. }
  461.  
  462.  
  463. ///--------------------------------------------------------------------------------------
  464. //    SWSwapSpriteLayer
  465. ///--------------------------------------------------------------------------------------
  466.  
  467. SW_FUNC void SWSwapSpriteLayer(
  468.     SpriteWorldPtr    spriteWorldP,
  469.     SpriteLayerPtr    srcSpriteLayerP,
  470.     SpriteLayerPtr    dstSpriteLayerP)
  471. {
  472.     register SpriteLayerPtr        swapSpriteLayerP;
  473.     
  474.     SW_ASSERT(spriteWorldP != NULL);
  475.     SW_ASSERT(srcSpriteLayerP != NULL && dstSpriteLayerP != NULL);
  476.     
  477.         // Do nothing if the sprite layers are the same
  478.     if (srcSpriteLayerP == dstSpriteLayerP)
  479.         return;
  480.  
  481.         // adjacent Layers are a special case
  482.     if ( srcSpriteLayerP->nextSpriteLayerP == dstSpriteLayerP ||
  483.         dstSpriteLayerP->nextSpriteLayerP == srcSpriteLayerP )
  484.     {
  485.         if ( srcSpriteLayerP->nextSpriteLayerP == dstSpriteLayerP )
  486.         {
  487.             if ( srcSpriteLayerP->prevSpriteLayerP != NULL )
  488.                 (srcSpriteLayerP->prevSpriteLayerP)->nextSpriteLayerP = dstSpriteLayerP;
  489.             if ( dstSpriteLayerP->nextSpriteLayerP != NULL )
  490.                 (dstSpriteLayerP->nextSpriteLayerP)->prevSpriteLayerP = srcSpriteLayerP;
  491.             
  492.             dstSpriteLayerP->prevSpriteLayerP = srcSpriteLayerP->prevSpriteLayerP;
  493.             srcSpriteLayerP->nextSpriteLayerP = dstSpriteLayerP->nextSpriteLayerP;
  494.             
  495.             dstSpriteLayerP->nextSpriteLayerP = srcSpriteLayerP;
  496.             srcSpriteLayerP->prevSpriteLayerP = dstSpriteLayerP;
  497.         }
  498.         else
  499.         {
  500.             if ( dstSpriteLayerP->prevSpriteLayerP != NULL )
  501.                 (dstSpriteLayerP->prevSpriteLayerP)->nextSpriteLayerP = srcSpriteLayerP;
  502.             if ( srcSpriteLayerP->nextSpriteLayerP != NULL )
  503.                 (srcSpriteLayerP->nextSpriteLayerP)->prevSpriteLayerP = dstSpriteLayerP;
  504.             
  505.             srcSpriteLayerP->prevSpriteLayerP = dstSpriteLayerP->prevSpriteLayerP;
  506.             dstSpriteLayerP->nextSpriteLayerP = srcSpriteLayerP->nextSpriteLayerP;
  507.             
  508.             srcSpriteLayerP->nextSpriteLayerP = dstSpriteLayerP;
  509.             dstSpriteLayerP->prevSpriteLayerP = srcSpriteLayerP;
  510.         }
  511.     }
  512.     else
  513.     {
  514.         if (srcSpriteLayerP->prevSpriteLayerP != NULL)
  515.             srcSpriteLayerP->prevSpriteLayerP->nextSpriteLayerP = dstSpriteLayerP;
  516.         if (dstSpriteLayerP->prevSpriteLayerP != NULL)
  517.             dstSpriteLayerP->prevSpriteLayerP->nextSpriteLayerP = srcSpriteLayerP;
  518.         if (srcSpriteLayerP->nextSpriteLayerP != NULL)
  519.             srcSpriteLayerP->nextSpriteLayerP->prevSpriteLayerP = dstSpriteLayerP;
  520.         if (dstSpriteLayerP->nextSpriteLayerP != NULL)
  521.             dstSpriteLayerP->nextSpriteLayerP->prevSpriteLayerP = srcSpriteLayerP;
  522.  
  523.         swapSpriteLayerP = srcSpriteLayerP->nextSpriteLayerP;
  524.         srcSpriteLayerP->nextSpriteLayerP = dstSpriteLayerP->nextSpriteLayerP;
  525.         dstSpriteLayerP->nextSpriteLayerP = swapSpriteLayerP;
  526.  
  527.         swapSpriteLayerP = srcSpriteLayerP->prevSpriteLayerP;
  528.         srcSpriteLayerP->prevSpriteLayerP = dstSpriteLayerP->prevSpriteLayerP;
  529.         dstSpriteLayerP->prevSpriteLayerP = swapSpriteLayerP;
  530.     }
  531.  
  532.     if (srcSpriteLayerP->nextSpriteLayerP == NULL)
  533.     {
  534.         spriteWorldP->tailSpriteLayerP = srcSpriteLayerP;
  535.     }
  536.     else if (srcSpriteLayerP->prevSpriteLayerP == NULL)
  537.     {
  538.         spriteWorldP->headSpriteLayerP = srcSpriteLayerP;
  539.     }
  540.  
  541.     if (dstSpriteLayerP->nextSpriteLayerP == NULL)
  542.     {
  543.         spriteWorldP->tailSpriteLayerP = dstSpriteLayerP;
  544.     }
  545.     else if (dstSpriteLayerP->prevSpriteLayerP == NULL)
  546.     {
  547.         spriteWorldP->headSpriteLayerP = dstSpriteLayerP;
  548.     }
  549. }
  550.  
  551.  
  552. ///--------------------------------------------------------------------------------------
  553. //    SWGetNextSpriteLayer
  554. ///--------------------------------------------------------------------------------------
  555.  
  556. SW_FUNC SpriteLayerPtr SWGetNextSpriteLayer(
  557.     SpriteWorldPtr    spriteWorldP,
  558.     SpriteLayerPtr    curSpriteLayerP)
  559. {
  560.     SW_ASSERT(spriteWorldP != NULL);
  561.     
  562.     return (curSpriteLayerP == NULL) ?
  563.             spriteWorldP->headSpriteLayerP :
  564.             curSpriteLayerP->nextSpriteLayerP;
  565. }
  566.  
  567.  
  568. ///--------------------------------------------------------------------------------------
  569. //    SWLockSpriteWorld
  570. ///--------------------------------------------------------------------------------------
  571.  
  572. SW_FUNC void SWLockSpriteWorld(
  573.     SpriteWorldPtr    spriteWorldP)
  574. {
  575.     SpriteLayerPtr curSpriteLayerP;
  576.  
  577.     SW_ASSERT(spriteWorldP != NULL);
  578.     
  579.     SWLockWindowFrame(spriteWorldP->windowFrameP);
  580.     SWLockFrame(spriteWorldP->workFrameP);
  581.     SWLockFrame(spriteWorldP->backFrameP);
  582.     if (spriteWorldP->extraBackFrameP != NULL)
  583.         SWLockFrame(spriteWorldP->extraBackFrameP);
  584.     
  585.     SWLockTiles(spriteWorldP);
  586.  
  587.     curSpriteLayerP = spriteWorldP->headSpriteLayerP;
  588.  
  589.     while (curSpriteLayerP != NULL)
  590.     {
  591.         SWLockSpriteLayer(curSpriteLayerP);
  592.  
  593.         curSpriteLayerP = curSpriteLayerP->nextSpriteLayerP;
  594.     }
  595. }
  596.  
  597.  
  598. ///--------------------------------------------------------------------------------------
  599. //    SWUnlockSpriteWorld
  600. ///--------------------------------------------------------------------------------------
  601.  
  602. SW_FUNC void SWUnlockSpriteWorld(
  603.     SpriteWorldPtr    spriteWorldP)
  604. {
  605.     SpriteLayerPtr curSpriteLayerP;
  606.     
  607.     SW_ASSERT(spriteWorldP != NULL);
  608.  
  609.     SWUnlockWindowFrame(spriteWorldP->windowFrameP);
  610.     SWUnlockFrame(spriteWorldP->backFrameP);
  611.     SWUnlockFrame(spriteWorldP->workFrameP);    
  612.     if (spriteWorldP->extraBackFrameP != NULL)
  613.         SWUnlockFrame(spriteWorldP->extraBackFrameP);
  614.     
  615.     SWUnlockTiles(spriteWorldP);
  616.  
  617.     curSpriteLayerP = spriteWorldP->headSpriteLayerP;
  618.  
  619.     while (curSpriteLayerP != NULL)
  620.     {
  621.         SWUnlockSpriteLayer(curSpriteLayerP);
  622.  
  623.         curSpriteLayerP = curSpriteLayerP->nextSpriteLayerP;
  624.     }
  625. }
  626.  
  627.  
  628. #pragma mark -
  629. ///--------------------------------------------------------------------------------------
  630. //    SWSetPortToBackground
  631. ///--------------------------------------------------------------------------------------
  632.  
  633. SW_FUNC void SWSetPortToBackground(
  634.     SpriteWorldPtr    spriteWorldP)
  635. {
  636.     SW_ASSERT(spriteWorldP != NULL);
  637.     
  638.     SWSetPortToFrame( spriteWorldP->backFrameP );
  639. }
  640.  
  641.  
  642. ///--------------------------------------------------------------------------------------
  643. //    SWSetPortToWorkArea
  644. ///--------------------------------------------------------------------------------------
  645.  
  646. SW_FUNC void SWSetPortToWorkArea(
  647.     SpriteWorldPtr    spriteWorldP)
  648. {
  649.     SW_ASSERT(spriteWorldP != NULL);
  650.     
  651.     SWSetPortToFrame( spriteWorldP->workFrameP );
  652. }
  653.  
  654.  
  655. ///--------------------------------------------------------------------------------------
  656. //    SWSetPortToWindow
  657. ///--------------------------------------------------------------------------------------
  658.  
  659. SW_FUNC void SWSetPortToWindow(
  660.     SpriteWorldPtr    spriteWorldP)
  661. {
  662.     SW_ASSERT(spriteWorldP != NULL);
  663.     
  664.     SWSetPortToFrame( spriteWorldP->windowFrameP );
  665. }
  666.  
  667.  
  668. ///--------------------------------------------------------------------------------------
  669. //    SWSetSpriteWorldOffscreenDrawProc
  670. ///--------------------------------------------------------------------------------------
  671.  
  672. SW_FUNC OSErr SWSetSpriteWorldOffscreenDrawProc(
  673.     SpriteWorldPtr    spriteWorldP,
  674.     DrawProcPtr        drawProc)
  675. {
  676.     OSErr    err = noErr;
  677.     
  678.     SW_ASSERT(spriteWorldP != NULL);
  679.     SW_ASSERT(drawProc != NULL);
  680.     
  681. #if SW_PPC
  682.     if (drawProc == BlitPixieAllBitRectDrawProc)
  683.         err = k68kOnlyErr;
  684. #endif
  685.     
  686.     if (spriteWorldP->pixelDepth < 8)
  687.     {
  688.         if ( drawProc == BlitPixieRectDrawProc )
  689.             err = kWrongDepthErr;
  690.     }
  691.  
  692.     if ( err == noErr )
  693.         spriteWorldP->offscreenDrawProc = drawProc;
  694.     
  695.     SWSetStickyIfError( err );
  696.     return err;
  697. }
  698.  
  699.  
  700. ///--------------------------------------------------------------------------------------
  701. //    SWSetSpriteWorldScreenDrawProc
  702. ///--------------------------------------------------------------------------------------
  703.  
  704. SW_FUNC OSErr SWSetSpriteWorldScreenDrawProc(
  705.     SpriteWorldPtr    spriteWorldP,
  706.     DrawProcPtr        drawProc)
  707. {
  708.     OSErr    err = noErr;
  709.     
  710.     SW_ASSERT(spriteWorldP != NULL);
  711.     SW_ASSERT(drawProc != NULL);
  712.     
  713. #if SW_PPC
  714.     if (drawProc == BlitPixieAllBitRectDrawProc)
  715.         err = k68kOnlyErr;
  716. #endif
  717.     
  718.     if ( (*spriteWorldP->windowFrameP->framePort->portPixMap)->pixelSize < 8 ||
  719.         (*spriteWorldP->windowFrameP->framePort->portPixMap)->pixelSize !=
  720.          spriteWorldP->pixelDepth )
  721.     {
  722.         if ( drawProc == BlitPixieRectDrawProc )
  723.             err = kWrongDepthErr;
  724.     }
  725.  
  726.     if ( err == noErr )
  727.         spriteWorldP->screenDrawProc = drawProc;
  728.         
  729.     SWSetStickyIfError( err );
  730.     return err;
  731. }
  732.  
  733.  
  734. ///--------------------------------------------------------------------------------------
  735. //    SWSetPostEraseCallBack
  736. ///--------------------------------------------------------------------------------------
  737.  
  738. SW_FUNC void SWSetPostEraseCallBack(
  739.     SpriteWorldPtr    spriteWorldP,
  740.     CallBackPtr        callBack)
  741. {
  742.     spriteWorldP->postEraseCallBack = callBack;
  743. }
  744.  
  745.  
  746. ///--------------------------------------------------------------------------------------
  747. //    SWSetPostDrawCallBack
  748. ///--------------------------------------------------------------------------------------
  749.  
  750. SW_FUNC void SWSetPostDrawCallBack(
  751.     SpriteWorldPtr    spriteWorldP,
  752.     CallBackPtr        callBack)
  753. {
  754.     spriteWorldP->postDrawCallBack = callBack;
  755. }
  756.  
  757.  
  758. ///--------------------------------------------------------------------------------------
  759. //    SWSetSpriteWorldMaxFPS
  760. ///--------------------------------------------------------------------------------------
  761.  
  762. SW_FUNC void SWSetSpriteWorldMaxFPS(
  763.     SpriteWorldPtr spriteWorldP,
  764.     short framesPerSec)
  765. {
  766.     SW_ASSERT(spriteWorldP != NULL);
  767.     
  768.         // is framesPerSec a valid value?
  769.     if (framesPerSec > 0 && framesPerSec <= 1000 )
  770.     {
  771.         spriteWorldP->fpsTimeInterval = 1000/framesPerSec;
  772.     }
  773.         // framesPerSec is an "ignore it" value.
  774.     else
  775.     {
  776.         spriteWorldP->fpsTimeInterval = 0;
  777.     }
  778. }
  779.  
  780.  
  781. ///--------------------------------------------------------------------------------------
  782. //    SWSetBackgroundColor
  783. ///--------------------------------------------------------------------------------------
  784.  
  785. SW_FUNC void SWSetBackgroundColor(
  786.     SpriteWorldPtr spriteWorldP,
  787.     RGBColor *color)
  788. {
  789.     SW_ASSERT(spriteWorldP != NULL);
  790.     SW_ASSERT(spriteWorldP->workFrameP != NULL);
  791.  
  792.     spriteWorldP->backgroundColor = *color;
  793.     spriteWorldP->backgroundValue = SWGetValueFromColor( spriteWorldP->workFrameP, color );
  794. }
  795.  
  796.  
  797. ///--------------------------------------------------------------------------------------
  798. //    SWStdWorldClearDrawProc - calls RGBBackColor and EraseRect
  799. ///--------------------------------------------------------------------------------------
  800.  
  801. SW_FUNC void SWStdWorldClearDrawProc(
  802.     FramePtr    srcFrameP,
  803.     FramePtr    dstFrameP,
  804.     Rect*        srcRect,
  805.     Rect*        dstRect)
  806. {
  807.     #pragma unused(srcFrameP,srcRect)
  808.     Rect    dstCopyBitsRect = *dstRect;
  809.  
  810.         // clip off the top
  811.     if (dstCopyBitsRect.top < dstFrameP->frameRect.top)
  812.     {
  813.         dstCopyBitsRect.top =  dstFrameP->frameRect.top;
  814.         if (dstCopyBitsRect.top >= dstCopyBitsRect.bottom) return;
  815.     }
  816.         // clip off the bottom
  817.     if (dstCopyBitsRect.bottom > dstFrameP->frameRect.bottom)
  818.     {
  819.         dstCopyBitsRect.bottom = dstFrameP->frameRect.bottom;
  820.         if (dstCopyBitsRect.bottom <= dstCopyBitsRect.top) return;
  821.     }
  822.         // clip off the left
  823.     if (dstCopyBitsRect.left < dstFrameP->frameRect.left)
  824.     {
  825.         dstCopyBitsRect.left = dstFrameP->frameRect.left;
  826.         if (dstCopyBitsRect.left >= dstCopyBitsRect.right) 
  827.             return;
  828.     }
  829.         // clip off the right
  830.     if (dstCopyBitsRect.right > dstFrameP->frameRect.right)
  831.     {
  832.         dstCopyBitsRect.right = dstFrameP->frameRect.right;
  833.         if (dstCopyBitsRect.right <= dstCopyBitsRect.left) return;
  834.     }    
  835.  
  836.     SW_ASSERT( gSWCurrentSpriteWorld != NULL );
  837.  
  838.     RGBBackColor( &gSWCurrentSpriteWorld->backgroundColor );
  839.     EraseRect( &dstCopyBitsRect );
  840.     BackColor(whiteColor);
  841. }
  842.  
  843.  
  844. ///--------------------------------------------------------------------------------------
  845. //    SWStdWorldDrawProc - calls CopyBits.
  846. ///--------------------------------------------------------------------------------------
  847.  
  848. SW_FUNC void SWStdWorldDrawProc(
  849.     FramePtr    srcFrameP,
  850.     FramePtr    dstFrameP,
  851.     Rect*        srcRect,
  852.     Rect*        dstRect)
  853. {
  854.     Rect    srcCopyBitsRect = *srcRect;
  855.     Rect    dstCopyBitsRect = *dstRect;
  856.  
  857.         // clip off the top
  858.     if (dstCopyBitsRect.top < dstFrameP->frameRect.top)
  859.     {
  860.         srcCopyBitsRect.top += dstFrameP->frameRect.top - dstCopyBitsRect.top;
  861.         dstCopyBitsRect.top =  dstFrameP->frameRect.top;
  862.         if (dstCopyBitsRect.top >= dstCopyBitsRect.bottom) return;
  863.     }
  864.         // clip off the bottom
  865.     if (dstCopyBitsRect.bottom > dstFrameP->frameRect.bottom)
  866.     {
  867.         srcCopyBitsRect.bottom -= dstCopyBitsRect.bottom - dstFrameP->frameRect.bottom;
  868.         dstCopyBitsRect.bottom = dstFrameP->frameRect.bottom;
  869.         if (dstCopyBitsRect.bottom <= dstCopyBitsRect.top) return;
  870.     }
  871.         // clip off the left
  872.     if (dstCopyBitsRect.left < dstFrameP->frameRect.left)
  873.     {
  874.         srcCopyBitsRect.left += dstFrameP->frameRect.left - dstCopyBitsRect.left;
  875.         dstCopyBitsRect.left = dstFrameP->frameRect.left;
  876.         if (dstCopyBitsRect.left >= dstCopyBitsRect.right) 
  877.             return;
  878.     }
  879.         // clip off the right
  880.     if (dstCopyBitsRect.right > dstFrameP->frameRect.right)
  881.     {
  882.         srcCopyBitsRect.right -= dstCopyBitsRect.right - dstFrameP->frameRect.right;
  883.         dstCopyBitsRect.right = dstFrameP->frameRect.right;
  884.         if (dstCopyBitsRect.right <= dstCopyBitsRect.left) return;
  885.     }    
  886.  
  887.     CopyBits(
  888.         (BitMap*)srcFrameP->framePix,
  889.         (BitMap*)dstFrameP->framePix,
  890.         &srcCopyBitsRect,
  891.         &dstCopyBitsRect,
  892.         srcCopy, NULL);
  893. }
  894.  
  895.  
  896. ///--------------------------------------------------------------------------------------
  897. //    SWInterlacedCopyBitsDrawProc - does interlaced CopyBits if interlacing is on
  898. //
  899. //  thanks to George Warner and Steve Ramsey for the code this is based on.
  900. //
  901. //    Note:    This is a hack, and might break in the future.
  902. //            It does not honor the clipRgn or visRgn of the dest port,
  903. //            and disables any hardware acceleration for CopyBits.
  904. ///--------------------------------------------------------------------------------------
  905.  
  906. SW_FUNC void SWInterlacedCopyBitsDrawProc(
  907.     FramePtr    srcFrameP,
  908.     FramePtr    dstFrameP,
  909.     Rect*        srcRect,
  910.     Rect*        dstRect)
  911. {
  912.     PixMap         srcMap = *srcFrameP->framePix;
  913.     PixMap         dstMap = *dstFrameP->framePix;
  914.     Rect        srcCopyBitsRect = *srcRect;
  915.     Rect        dstCopyBitsRect = *dstRect;
  916.     UInt32        srcRowBytes, dstRowBytes;
  917.     SInt32        sourceTopShift, destTopShift;
  918.  
  919.         // clip off the top
  920.     if (dstCopyBitsRect.top < dstFrameP->frameRect.top)
  921.     {
  922.         srcCopyBitsRect.top += dstFrameP->frameRect.top - dstCopyBitsRect.top;
  923.         dstCopyBitsRect.top =  dstFrameP->frameRect.top;
  924.         if (dstCopyBitsRect.top >= dstCopyBitsRect.bottom) return;
  925.     }
  926.         // clip off the bottom
  927.     if (dstCopyBitsRect.bottom > dstFrameP->frameRect.bottom)
  928.     {
  929.         srcCopyBitsRect.bottom -= dstCopyBitsRect.bottom - dstFrameP->frameRect.bottom;
  930.         dstCopyBitsRect.bottom = dstFrameP->frameRect.bottom;
  931.         if (dstCopyBitsRect.bottom <= dstCopyBitsRect.top) return;
  932.     }
  933.         // clip off the left
  934.     if (dstCopyBitsRect.left < dstFrameP->frameRect.left)
  935.     {
  936.         srcCopyBitsRect.left += dstFrameP->frameRect.left - dstCopyBitsRect.left;
  937.         dstCopyBitsRect.left = dstFrameP->frameRect.left;
  938.         if (dstCopyBitsRect.left >= dstCopyBitsRect.right) 
  939.             return;
  940.     }
  941.         // clip off the right
  942.     if (dstCopyBitsRect.right > dstFrameP->frameRect.right)
  943.     {
  944.         srcCopyBitsRect.right -= dstCopyBitsRect.right - dstFrameP->frameRect.right;
  945.         dstCopyBitsRect.right = dstFrameP->frameRect.right;
  946.         if (dstCopyBitsRect.right <= dstCopyBitsRect.left) return;
  947.     }    
  948.  
  949.     if ( dstFrameP->interlacingIsOn )
  950.     {
  951.             //    Get the original bytes per row (ignore top two flag bits)
  952.         srcRowBytes = srcMap.rowBytes & 0x3FFF;
  953.         dstRowBytes = dstMap.rowBytes & 0x3FFF;
  954.  
  955.         //  back up 2 rows
  956.         //
  957.         //  This prevents QuickDraw from using an accelerated (hardware)
  958.         //  blitter that's optimized to go directly to VRAM (which might ignore
  959.         //  our hacked rowBytes value). Most of these accelerators compare base
  960.         //  addresses against their VRAM base to determine when they can
  961.         //  accelerate. I'm just messing up this compare.
  962.         //
  963.         //  Backing up two full rows ensures that alignment stays the same and
  964.         //  that the even/odd relationships of the bounding Rects and blit Rects
  965.         //  stays the same.
  966.         {
  967.             srcMap.baseAddr -= (srcRowBytes + srcRowBytes);
  968.             srcMap.bounds.top -= 2;
  969.             srcMap.bounds.bottom -= 2;
  970.  
  971.             dstMap.baseAddr -= (dstRowBytes + dstRowBytes);
  972.             dstMap.bounds.top -= 2;
  973.             dstMap.bounds.bottom -= 2;
  974.         }
  975.  
  976.             // flips whether even or odd scanlines are being drawn this frame
  977.         if (!dstFrameP->skipOddLines) // i.e. we're drawing the odd lines
  978.         {
  979.                 //    Skip first line if it is even
  980.             if ( !((dstCopyBitsRect.top - dstFrameP->frameRect.top) & 1 ))
  981.             {
  982.                 srcCopyBitsRect.top++;
  983.                 dstCopyBitsRect.top++;
  984.             } 
  985.  
  986.                 // This bit of adjustment is required to ensure that we're going
  987.                 // to be working with the right set of scanlines. Otherwise, once
  988.                 // the rowBytes have doubled, we would have no way of getting at
  989.                 // the "in-between" scanlines.
  990.             srcMap.baseAddr += srcRowBytes;
  991.             dstMap.baseAddr += dstRowBytes;
  992.  
  993.             srcCopyBitsRect.top--;
  994.             dstCopyBitsRect.top--;
  995.  
  996.                 // Adjust the blit Rect sizes to eliminate rounding errors that
  997.                 // can be introduced if the Rect height is odd. For the odd scanline
  998.                 // pass we force the adjusted Rect height to be floor'ed.
  999.             if ((srcCopyBitsRect.bottom - srcCopyBitsRect.top) & 1)
  1000.                 srcCopyBitsRect.bottom--;
  1001.  
  1002.             if ((dstCopyBitsRect.bottom - dstCopyBitsRect.top) & 1)
  1003.                 dstCopyBitsRect.bottom--;
  1004.         }
  1005.         else // i.e. we're drawing the even lines
  1006.         {
  1007.                 //    Skip first line if it is odd
  1008.             if ( (dstCopyBitsRect.top -  dstFrameP->frameRect.top) & 1 )
  1009.             {
  1010.                 srcCopyBitsRect.top++;
  1011.                 dstCopyBitsRect.top++;
  1012.             }
  1013.  
  1014.                 //  Adjust the blit Rect sizes to eliminate rounding errors that
  1015.                 //  can be introduced if the Rect height is odd. For the even
  1016.                 //  scanline pass we force the adjusted Rect height to be ceiling'ed.
  1017.             if ((srcCopyBitsRect.bottom - srcCopyBitsRect.top) & 1)
  1018.                 srcCopyBitsRect.bottom++;
  1019.  
  1020.             if ((dstCopyBitsRect.bottom - dstCopyBitsRect.top) & 1)
  1021.                 dstCopyBitsRect.bottom++;
  1022.         }
  1023.  
  1024.         //  Define vertical shift values. Vertical shifting may be necessary if
  1025.         //  a rounding error gets introduced in the coming calculations. Both the
  1026.         //  srcPixMapP/destPixMapP rectangles and the baseAddress must be
  1027.         //  shifted. Note that this would not be necessary if the Rects to be
  1028.         //  blitted were restricted to even top values.
  1029.         sourceTopShift = (srcCopyBitsRect.top - srcMap.bounds.top) & 1;
  1030.         destTopShift = (dstCopyBitsRect.top - dstMap.bounds.top) & 1;
  1031.  
  1032.         if (sourceTopShift)
  1033.             srcMap.baseAddr -= srcRowBytes;
  1034.  
  1035.         if (destTopShift)
  1036.             dstMap.baseAddr -= dstRowBytes;
  1037.          
  1038.             // scale the source rect to reflect the new rowBytes value
  1039.         srcCopyBitsRect.bottom = (srcCopyBitsRect.bottom - srcMap.bounds.top) >> 1;
  1040.         srcCopyBitsRect.top = (srcCopyBitsRect.top - srcMap.bounds.top) >> 1;
  1041.  
  1042.             // scale the source PixMap bounds to reflect the new rowBytes value
  1043.         srcMap.bounds.top >>= 1;
  1044.         srcMap.bounds.bottom >>= 1;
  1045.  
  1046.             // scale the dest rect to reflect the new rowBytes value
  1047.         dstCopyBitsRect.bottom = (dstCopyBitsRect.bottom - dstMap.bounds.top) >> 1;
  1048.         dstCopyBitsRect.top = (dstCopyBitsRect.top - dstMap.bounds.top) >> 1;
  1049.  
  1050.             // scale the dest PixMap bounds to reflect the new rowBytes value
  1051.         dstMap.bounds.top >>= 1;
  1052.         dstMap.bounds.bottom >>= 1;
  1053.  
  1054.             // translate the source rect to reflect a change in the srcMap bounds origin
  1055.         srcCopyBitsRect.bottom += (srcMap.bounds.top + sourceTopShift);
  1056.         srcCopyBitsRect.top += (srcMap.bounds.top + sourceTopShift);
  1057.  
  1058.             // translate the dest rect to reflect a change in the destMap bounds origin
  1059.         dstCopyBitsRect.bottom += (dstMap.bounds.top + destTopShift);
  1060.         dstCopyBitsRect.top += (dstMap.bounds.top + destTopShift);
  1061.  
  1062.         srcCopyBitsRect.bottom += sourceTopShift;
  1063.         srcCopyBitsRect.top += sourceTopShift;
  1064.         dstCopyBitsRect.bottom += sourceTopShift;
  1065.         dstCopyBitsRect.top += sourceTopShift;
  1066.        
  1067.         srcMap.bounds.bottom += 1;
  1068.         dstMap.bounds.bottom += 1;
  1069.  
  1070.             // double source rowBytes (keep the top two flag bits)
  1071.         srcRowBytes += srcRowBytes;
  1072.         srcMap.rowBytes = (srcMap.rowBytes & 0xC000) | (srcRowBytes & 0x3FFF);
  1073.  
  1074.             // double destination rowBytes (keep the top two flag bits)
  1075.         dstRowBytes += dstRowBytes;
  1076.         dstMap.rowBytes = (dstMap.rowBytes & 0xC000) | (dstRowBytes & 0x3FFF);
  1077.  
  1078.         if (srcCopyBitsRect.top >= srcCopyBitsRect.bottom) return;
  1079.         if (dstCopyBitsRect.top >= dstCopyBitsRect.bottom) return;
  1080.  
  1081.             // and finally call (software) CopyBits to do the interlaced blit
  1082.         CopyBits(
  1083.             (BitMap*)&srcMap,
  1084.             (BitMap*)&dstMap,
  1085.             &srcCopyBitsRect,
  1086.             &dstCopyBitsRect,
  1087.             srcCopy, NULL);
  1088.     }
  1089.     else
  1090.     {
  1091.         CopyBits(
  1092.             (BitMap*)srcFrameP->framePix,
  1093.             (BitMap*)dstFrameP->framePix,
  1094.             &srcCopyBitsRect,
  1095.             &dstCopyBitsRect,
  1096.             srcCopy, NULL);
  1097.     }
  1098. }
  1099.  
  1100.  
  1101.  
  1102. ///--------------------------------------------------------------------------------------
  1103. //    SWCopyBackgroundToWorkArea
  1104. ///--------------------------------------------------------------------------------------
  1105.  
  1106. SW_FUNC void SWCopyBackgroundToWorkArea(
  1107.     SpriteWorldPtr spriteWorldP)
  1108. {        
  1109.     GWorldPtr        holdGWorld;
  1110.     GDHandle        holdGDH;
  1111.     
  1112.     SW_ASSERT(spriteWorldP != NULL);
  1113.     SW_ASSERT(spriteWorldP->backFrameP->isFrameLocked);
  1114.     SW_ASSERT(spriteWorldP->workFrameP->isFrameLocked);
  1115.     
  1116.     GetGWorld( &holdGWorld, &holdGDH );
  1117.     
  1118.     SWSetPortToFrame( spriteWorldP->workFrameP );
  1119.  
  1120.         // Copy the background frame into the work area
  1121.     (*spriteWorldP->offscreenDrawProc)(spriteWorldP->backFrameP,
  1122.                                 spriteWorldP->workFrameP,
  1123.                                 &spriteWorldP->backFrameP->frameRect,
  1124.                                 &spriteWorldP->workFrameP->frameRect);
  1125.     
  1126.     SetGWorld( holdGWorld, holdGDH );
  1127. }
  1128.  
  1129.  
  1130. ///--------------------------------------------------------------------------------------
  1131. //    SWChangeWorldRect
  1132. ///--------------------------------------------------------------------------------------
  1133.  
  1134. SW_FUNC OSErr SWChangeWorldRect(
  1135.     SpriteWorldPtr spriteWorldP, 
  1136.     Rect* newWorldRectP,
  1137.     Boolean changeOffscreenAreas)
  1138. {
  1139.     Rect        oldWindRect;
  1140.     short        hChange, vChange;
  1141.     OSErr        err = noErr;
  1142.     
  1143.     SW_ASSERT(spriteWorldP != NULL && newWorldRectP != NULL);
  1144.     
  1145.         // Report error if newWorldRectP is larger than the size of the offscreen areas
  1146.     if ( SW_RECT_WIDTH((*newWorldRectP)) > SW_RECT_WIDTH(spriteWorldP->originalBackRect) ||
  1147.          SW_RECT_HEIGHT((*newWorldRectP)) > SW_RECT_HEIGHT(spriteWorldP->originalBackRect) )
  1148.     {
  1149.         err = kOutOfRangeErr;
  1150.     }
  1151.         
  1152.     
  1153.     if (err == noErr)
  1154.     {
  1155.             // Now we calculate the difference in size of old and new windRect
  1156.         oldWindRect = spriteWorldP->windRect;
  1157.         hChange = (oldWindRect.right - oldWindRect.left) - 
  1158.                     (newWorldRectP->right - newWorldRectP->left);
  1159.         vChange = (oldWindRect.bottom - oldWindRect.top) - 
  1160.                     (newWorldRectP->bottom - newWorldRectP->top);
  1161.         
  1162.             // Change the windRect
  1163.         spriteWorldP->windRect = *newWorldRectP;
  1164.         
  1165.             // Resize the visScrollRect
  1166.         SWResizeVisScrollRect(spriteWorldP,
  1167.                 newWorldRectP->right - newWorldRectP->left, 
  1168.                 newWorldRectP->bottom - newWorldRectP->top);
  1169.         
  1170.         if (changeOffscreenAreas)
  1171.         {
  1172.                 // Make offscreen areas think they are the same size as the new windRect
  1173.             spriteWorldP->workFrameP->frameRect.bottom -= vChange;
  1174.             spriteWorldP->workFrameP->frameRect.right -= hChange;
  1175.             spriteWorldP->backFrameP->frameRect.bottom -= vChange;
  1176.             spriteWorldP->backFrameP->frameRect.right -= hChange;
  1177.             
  1178.                 // Update backRect as well
  1179.             spriteWorldP->backRect.bottom -= vChange;
  1180.             spriteWorldP->backRect.right -= hChange;
  1181.         }
  1182.         
  1183.             // Recalculate blitter data based on new windRect position
  1184.         SWWindowMoved(spriteWorldP);
  1185.     }
  1186.     
  1187.     return err;
  1188. }
  1189.  
  1190.  
  1191. ///--------------------------------------------------------------------------------------
  1192. //    SWRestoreWorldRect
  1193. ///--------------------------------------------------------------------------------------
  1194.  
  1195. SW_FUNC void SWRestoreWorldRect(SpriteWorldPtr spriteWorldP)
  1196. {
  1197.     Rect    originalWindRect = spriteWorldP->originalWindRect;
  1198.     
  1199.     SW_ASSERT(spriteWorldP != NULL);
  1200.     
  1201.         // Restore everything to its original size
  1202.     spriteWorldP->windRect = originalWindRect;
  1203.     spriteWorldP->workFrameP->frameRect = spriteWorldP->originalBackRect;
  1204.     spriteWorldP->backFrameP->frameRect = spriteWorldP->originalBackRect;
  1205.     spriteWorldP->backRect = spriteWorldP->originalBackRect;
  1206.     
  1207.         // Resize the visScrollRect to match the originalWindRect
  1208.     SWResizeVisScrollRect(spriteWorldP,
  1209.             originalWindRect.right - originalWindRect.left, 
  1210.             originalWindRect.bottom - originalWindRect.top);
  1211.     
  1212.         // Recalculate blitter data based on new windRect position
  1213.     SWWindowMoved(spriteWorldP);
  1214. }
  1215.  
  1216.  
  1217. ///--------------------------------------------------------------------------------------
  1218. //    SWWindowMoved
  1219. ///--------------------------------------------------------------------------------------
  1220.  
  1221. SW_FUNC void SWWindowMoved(SpriteWorldPtr spriteWorldP)
  1222. {
  1223. //    Boolean        needRedraw = false;
  1224.     
  1225.     SWWindowFrameMoved( spriteWorldP->windowFrameP, &spriteWorldP->windRect );
  1226.  
  1227. /*    Commented out the aligning code, since it doesn't seem to work everywhere ? :-(
  1228.  
  1229.     if ( SWAlignFrameToWindow( spriteWorldP->workFrameP, spriteWorldP->windowFrameP ) )
  1230.         needRedraw = true;
  1231.     if ( SWAlignFrameToWindow( spriteWorldP->backFrameP, spriteWorldP->windowFrameP ) )
  1232.         needRedraw = true;
  1233.     if ( spriteWorldP->extraBackFrameP != NULL )
  1234.         if ( SWAlignFrameToWindow( spriteWorldP->extraBackFrameP, spriteWorldP->windowFrameP ) )
  1235.             needRedraw = true;
  1236.     
  1237.     if ( needRedraw )
  1238.     {
  1239.             // update tiles
  1240.         if ( spriteWorldP->tilingIsOn )
  1241.         {
  1242.             SWResetTilingCache( spriteWorldP );
  1243.             SWDrawTilesInBackground( spriteWorldP );
  1244.         }
  1245.         
  1246.             // update work area
  1247.         SWSetPortToWindow( spriteWorldP );
  1248.         if ( EqualRect(&spriteWorldP->windRect,&spriteWorldP->offscreenScrollRect ) )
  1249.             SWUpdateSpriteWorld( spriteWorldP, false );
  1250.         else
  1251.             SWUpdateScrollingSpriteWorld( spriteWorldP, false );
  1252.     }
  1253. */
  1254.  
  1255.     
  1256. }
  1257.  
  1258.  
  1259. ///--------------------------------------------------------------------------------------
  1260. //    SWWindowFrameMoved
  1261. ///--------------------------------------------------------------------------------------
  1262.     
  1263. SW_FUNC void SWWindowFrameMoved(
  1264.     FramePtr windowFrameP,
  1265.     Rect *frameRect)
  1266. {
  1267.     GrafPtr            savePort;
  1268.     Rect             globalWorldRect,
  1269.                     screenRect;
  1270.     GDHandle        maxDevice;
  1271.     
  1272.     SW_ASSERT(windowFrameP != NULL);
  1273.     SW_ASSERT(windowFrameP->isFrameLocked);
  1274.     
  1275.     GetPort( &savePort );
  1276.  
  1277.     globalWorldRect = *frameRect;
  1278.     SetPort( (GrafPtr) windowFrameP->framePort );
  1279.     LocalToGlobal( &(topLeft(globalWorldRect)) );
  1280.     LocalToGlobal( &(botRight(globalWorldRect)) );
  1281.  
  1282.     maxDevice = GetMaxDevice(&globalWorldRect);
  1283.     screenRect = (*maxDevice)->gdRect;
  1284.         
  1285.         // Clip worldRect with screenRect
  1286.     if (globalWorldRect.top < screenRect.top)
  1287.         globalWorldRect.top = screenRect.top;
  1288.     if (globalWorldRect.bottom > screenRect.bottom)
  1289.         globalWorldRect.bottom = screenRect.bottom;
  1290.     if (globalWorldRect.left < screenRect.left)
  1291.         globalWorldRect.left = screenRect.left;
  1292.     if (globalWorldRect.right > screenRect.right)
  1293.         globalWorldRect.right = screenRect.right;
  1294.     
  1295.         // Convert global rect back to local rect
  1296.     GlobalToLocal( &(topLeft(globalWorldRect)) );
  1297.     GlobalToLocal( &(botRight(globalWorldRect)) );
  1298.     
  1299.     windowFrameP->frameRect = globalWorldRect;
  1300.     
  1301.         // frame blitter stuff needs to be re-calced, since things might have changed
  1302.     if ( windowFrameP->isFrameLocked )
  1303.     {
  1304.         SWUnlockWindowFrame( windowFrameP );
  1305.         SWLockWindowFrame( windowFrameP );
  1306.     }
  1307.  
  1308.     SetPort( savePort );
  1309. }
  1310.  
  1311.  
  1312. ///--------------------------------------------------------------------------------------
  1313. //    SWUpdateWindow
  1314. ///--------------------------------------------------------------------------------------
  1315.  
  1316. SW_FUNC void SWUpdateWindow(
  1317.     SpriteWorldPtr spriteWorldP)
  1318. {
  1319.     GWorldPtr        holdGWorld;
  1320.     GDHandle        holdGDH;
  1321.     
  1322.     SW_ASSERT(spriteWorldP != NULL);
  1323.     SW_ASSERT(spriteWorldP->workFrameP->isFrameLocked);
  1324.     SW_ASSERT(spriteWorldP->windowFrameP->isFrameLocked);
  1325.     
  1326.     GetGWorld( &holdGWorld, &holdGDH );
  1327.     
  1328.     SWSetPortToFrame( spriteWorldP->windowFrameP );
  1329.     spriteWorldP->numTilesChanged = 0;
  1330.     
  1331.     if ( spriteWorldP->usingVBL )
  1332.     {  
  1333.         spriteWorldP->vblTaskRec.hasVBLFired = false;
  1334.         while ( !spriteWorldP->vblTaskRec.hasVBLFired )
  1335.         {}
  1336.     }
  1337.     
  1338.     (*spriteWorldP->screenDrawProc)(spriteWorldP->workFrameP,
  1339.                                     spriteWorldP->windowFrameP,
  1340.                                     &spriteWorldP->visScrollRect,
  1341.                                     &spriteWorldP->windowFrameP->frameRect);
  1342.                                     
  1343.     SetGWorld( holdGWorld, holdGDH );
  1344. }
  1345.  
  1346.  
  1347. #pragma mark -
  1348. ///--------------------------------------------------------------------------------------
  1349. //    SWUpdateSpriteWorld
  1350. ///--------------------------------------------------------------------------------------
  1351.  
  1352. SW_FUNC void SWUpdateSpriteWorld(
  1353.     SpriteWorldPtr spriteWorldP,
  1354.     Boolean updateWindow)
  1355. {
  1356.     UpdateRectStructPtr            curRectStructP,
  1357.                                 nextRectStructP;
  1358.     register SpriteLayerPtr        curSpriteLayerP;
  1359.     register SpritePtr            curSpriteP;
  1360.     Rect                        tempRect;
  1361.     GWorldPtr                    holdGWorld;
  1362.     GDHandle                    holdGDH;
  1363.     short                        curTileLayer;
  1364.     
  1365.     SW_ASSERT(spriteWorldP != NULL);
  1366.     SW_ASSERT(spriteWorldP->backFrameP->isFrameLocked);
  1367.     SW_ASSERT(spriteWorldP->workFrameP->isFrameLocked);
  1368.     SW_ASSERT(spriteWorldP->windowFrameP->isFrameLocked);
  1369.     
  1370.     GetGWorld( &holdGWorld, &holdGDH );
  1371.     
  1372.     gSWCurrentSpriteWorld = spriteWorldP;
  1373.  
  1374.         // Copy the background into the work area    
  1375.     SWSetPortToFrame( spriteWorldP->workFrameP );
  1376.     (*spriteWorldP->offscreenDrawProc)(spriteWorldP->backFrameP,
  1377.                                        spriteWorldP->workFrameP,
  1378.                                        &spriteWorldP->backFrameP->frameRect,
  1379.                                        &spriteWorldP->workFrameP->frameRect);
  1380.     
  1381.             // Call the postEraseCallBack
  1382.     if (spriteWorldP->postEraseCallBack != NULL)
  1383.         (*spriteWorldP->postEraseCallBack)(spriteWorldP);
  1384.     
  1385.     
  1386.         // Build the current frame of the animation in the work area
  1387.  
  1388.     curSpriteLayerP = spriteWorldP->headSpriteLayerP;
  1389.     curTileLayer = 0;
  1390.  
  1391.         // iterate through the layers in this world
  1392.     while (curSpriteLayerP != NULL)
  1393.     {
  1394.         curSpriteP = curSpriteLayerP->headSpriteP;
  1395.         
  1396.         if (curSpriteLayerP->tileLayer > curTileLayer)
  1397.             curTileLayer = curSpriteLayerP->tileLayer;
  1398.  
  1399.             // iterate through the sprites in this layer
  1400.         while (curSpriteP != NULL)
  1401.         {
  1402.             SW_ASSERT(curSpriteP->curFrameP->isFrameLocked);
  1403.             curSpriteP->tileDepth = curTileLayer;
  1404.             
  1405.             if (curSpriteP->isVisible)
  1406.             {
  1407.                 gSWCurrentSpriteBeingDrawn = curSpriteP;
  1408.                 
  1409.                     // draw the sprite in the work area
  1410.                 (*curSpriteP->frameDrawProc)(curSpriteP->curFrameP,
  1411.                                             spriteWorldP->workFrameP,
  1412.                                             &curSpriteP->curFrameP->frameRect,
  1413.                                             &curSpriteP->destFrameRect);
  1414.                 
  1415.                 gSWCurrentSpriteBeingDrawn = NULL;
  1416.                 
  1417.                 if (spriteWorldP->tilingIsOn &&
  1418.                     curSpriteP->tileDepth <= spriteWorldP->lastActiveTileLayer)
  1419.                 {
  1420.                     tempRect = curSpriteP->destFrameRect;
  1421.                     
  1422.                         // Clip tempRect to visScrollRect
  1423.                     if (tempRect.left < spriteWorldP->visScrollRect.left)
  1424.                         tempRect.left = spriteWorldP->visScrollRect.left;
  1425.                     if (tempRect.right > spriteWorldP->visScrollRect.right)
  1426.                         tempRect.right = spriteWorldP->visScrollRect.right;
  1427.                     if (tempRect.top < spriteWorldP->visScrollRect.top)
  1428.                         tempRect.top = spriteWorldP->visScrollRect.top;
  1429.                     if (tempRect.bottom > spriteWorldP->visScrollRect.bottom)
  1430.                         tempRect.bottom = spriteWorldP->visScrollRect.bottom;
  1431.     
  1432.                     SWDrawTilesAboveSprite(spriteWorldP, &tempRect, curSpriteP->tileDepth);
  1433.                 }
  1434.             
  1435.             }
  1436.             
  1437.                 // set the delta and last rect to the current rect
  1438.             curSpriteP->deltaFrameRect = curSpriteP->destFrameRect;
  1439.             curSpriteP->oldFrameRect = curSpriteP->destFrameRect;
  1440.             
  1441.                 // this sprite no longer needs to be drawn
  1442.             curSpriteP->needsToBeDrawn = false;
  1443.             curSpriteP->needsToBeErased = false;
  1444.  
  1445.             curSpriteP = curSpriteP->nextSpriteP;
  1446.         }
  1447.  
  1448.         curSpriteLayerP = curSpriteLayerP->nextSpriteLayerP;
  1449.     }
  1450.     
  1451.     
  1452.         // Call the postDrawCallBack
  1453.     if (spriteWorldP->postDrawCallBack != NULL)
  1454.         (*spriteWorldP->postDrawCallBack)(spriteWorldP);
  1455.         
  1456.     
  1457.         // Copy the work area into the window
  1458.     if (updateWindow)
  1459.     {
  1460.         SWSetPortToFrame( spriteWorldP->windowFrameP );
  1461.     
  1462.         if ( spriteWorldP->usingVBL )
  1463.         {  
  1464.             spriteWorldP->vblTaskRec.hasVBLFired = false;
  1465.             while ( !spriteWorldP->vblTaskRec.hasVBLFired )
  1466.             {}
  1467.         }
  1468.         
  1469.         (*spriteWorldP->screenDrawProc)(spriteWorldP->workFrameP,
  1470.                                         spriteWorldP->windowFrameP,
  1471.                                         &spriteWorldP->visScrollRect,
  1472.                                         &spriteWorldP->windowFrameP->frameRect);
  1473.     }
  1474.     
  1475.     
  1476.         // dispose of flagged background rects
  1477.     nextRectStructP = spriteWorldP->headUpdateRectP;
  1478.     while ( nextRectStructP != NULL )
  1479.     {
  1480.         curRectStructP = nextRectStructP;
  1481.         nextRectStructP = curRectStructP->nextRectStructP;
  1482.         DisposePtr( (Ptr)curRectStructP );
  1483.     }
  1484.     spriteWorldP->headUpdateRectP = NULL;
  1485.     
  1486.     spriteWorldP->numTilesChanged = 0;
  1487.  
  1488.     gSWCurrentSpriteWorld = NULL;
  1489.     
  1490.     SetGWorld( holdGWorld, holdGDH );
  1491. }
  1492.  
  1493.  
  1494. ///--------------------------------------------------------------------------------------
  1495. //    SWProcessSpriteWorld - IMPORTANT: if you ever change this function, change 
  1496. //    SWProcessSpriteLayer to match this. This could call SWProcessSpriteLayer, but currently
  1497. //    the code is placed here directly to make it a little faster. Also, unlike 
  1498. //    SWProcessSpriteLayer, this function does *not* process non-scrolling layers. So if this
  1499. //    called SWProcessSpriteLayer, we'd have to make sure it's a non-scrolling layer first.
  1500. ///--------------------------------------------------------------------------------------
  1501.  
  1502. SW_FUNC void SWProcessSpriteWorld(
  1503.     SpriteWorldPtr spriteWorldP)
  1504. {
  1505.     UnsignedWide                curMicroseconds;
  1506.     register unsigned long        millisecDifference;
  1507.     register SpriteLayerPtr        curSpriteLayerP;
  1508.     register SpritePtr            curSpriteP, nextSpriteP;
  1509.     register FramePtr            newFrameP;
  1510.     
  1511.     SW_ASSERT(spriteWorldP != NULL);
  1512.  
  1513.     gSWCurrentSpriteWorld = spriteWorldP;
  1514.     
  1515.     Microseconds( &curMicroseconds );
  1516.         // has hi long has rolled over?
  1517.     if ( curMicroseconds.hi > spriteWorldP->lastMicroseconds.hi )
  1518.     {
  1519.         millisecDifference = 
  1520.             (0xffffffff - (spriteWorldP->lastMicroseconds.lo - curMicroseconds.lo))/1000;
  1521.     }
  1522.     else
  1523.     {
  1524.         millisecDifference = (curMicroseconds.lo - spriteWorldP->lastMicroseconds.lo)/1000;
  1525.     }
  1526.     
  1527.     if ( millisecDifference > 0 )
  1528.     {
  1529.         spriteWorldP->runningTimeCount += millisecDifference;
  1530.         spriteWorldP->lastMicroseconds.lo = curMicroseconds.lo;
  1531.         spriteWorldP->lastMicroseconds.hi = curMicroseconds.hi;
  1532.     }
  1533.  
  1534.     if ( (spriteWorldP->runningTimeCount - spriteWorldP->timeOfLastFrame >= 
  1535.                 spriteWorldP->fpsTimeInterval) )
  1536.     {
  1537.         spriteWorldP->frameHasOccurred = true;
  1538.         spriteWorldP->timeOfLastFrame = spriteWorldP->runningTimeCount;
  1539.     }
  1540.     else
  1541.     {
  1542.         spriteWorldP->frameHasOccurred = false;
  1543.         return;
  1544.     }
  1545.  
  1546.     if ( spriteWorldP->deadSpriteLayerP->headSpriteP != NULL )
  1547.     {
  1548.         SWFindSpritesToBeRemoved( spriteWorldP );
  1549.     }
  1550.     
  1551.         // Call the tileChangeProc, if there is one
  1552.     if (spriteWorldP->tileChangeProc != NULL)
  1553.     {
  1554.         (*spriteWorldP->tileChangeProc)(spriteWorldP);
  1555.     }
  1556.     
  1557.         // Set this to false before entering the loop below.
  1558.     spriteWorldP->thereAreNonScrollingLayers = false;
  1559.     
  1560.     curSpriteLayerP = spriteWorldP->headSpriteLayerP;    
  1561.     
  1562.         // iterate through the layers in this world
  1563.     while (curSpriteLayerP != NULL)
  1564.     {
  1565.         if (curSpriteLayerP->layerIsNonScrolling)
  1566.         {
  1567.             spriteWorldP->thereAreNonScrollingLayers = true;
  1568.         }
  1569.         else if (!curSpriteLayerP->isPaused)
  1570.         {
  1571.             curSpriteP = curSpriteLayerP->headSpriteP;
  1572.             
  1573.                 // iterate through the sprites in this layer
  1574.             while (curSpriteP != NULL)
  1575.             {            
  1576.                 SW_ASSERT(curSpriteP->curFrameP->isFrameLocked);
  1577.                 nextSpriteP = curSpriteP->nextSpriteP;
  1578.                 
  1579.                     // is it time to advance the sprite’s frame?
  1580.                 if ( curSpriteP->frameTimeInterval >= 0 && 
  1581.                     (spriteWorldP->runningTimeCount - curSpriteP->timeOfLastFrameChange) >=
  1582.                     curSpriteP->frameTimeInterval )
  1583.                 {
  1584.                     register long        currentFrameIndex;
  1585.                     
  1586.                     
  1587.                     curSpriteP->timeOfLastFrameChange = spriteWorldP->runningTimeCount;
  1588.                     
  1589.                     currentFrameIndex  = curSpriteP->curFrameIndex;
  1590.                     currentFrameIndex += curSpriteP->frameAdvance;
  1591.                     
  1592.                     if (currentFrameIndex < curSpriteP->firstFrameIndex)
  1593.                     {
  1594.                         if (curSpriteP->frameAdvanceMode == kSWWrapAroundMode)
  1595.                         {
  1596.                                 // wrap to the last frame
  1597.                             currentFrameIndex = curSpriteP->lastFrameIndex;
  1598.                         }
  1599.                         else    // curSpriteP->frameAdvanceMode == kSWPatrollingMode
  1600.                         {
  1601.                                 // change direction and set index to what it should be
  1602.                             curSpriteP->frameAdvance = -curSpriteP->frameAdvance;
  1603.                             currentFrameIndex += curSpriteP->frameAdvance;
  1604.                             currentFrameIndex += curSpriteP->frameAdvance;
  1605.                         }
  1606.                     }
  1607.                     else if (currentFrameIndex > curSpriteP->lastFrameIndex)
  1608.                     {
  1609.                         if (curSpriteP->frameAdvanceMode == kSWWrapAroundMode)
  1610.                         {
  1611.                                 // wrap to the first frame
  1612.                             currentFrameIndex = curSpriteP->firstFrameIndex;
  1613.                         }
  1614.                         else    // curSpriteP->frameAdvanceMode == kSWPatrollingMode
  1615.                         {
  1616.                                 // change direction and set index to what it should be
  1617.                             curSpriteP->frameAdvance = -curSpriteP->frameAdvance;
  1618.                             currentFrameIndex += curSpriteP->frameAdvance;
  1619.                             currentFrameIndex += curSpriteP->frameAdvance;
  1620.                         }
  1621.                     }
  1622.                     
  1623.                     curSpriteP->curFrameIndex = currentFrameIndex;
  1624.                     
  1625.                     
  1626.                         // is there a frame callback?
  1627.                     if (curSpriteP->frameChangeProc != NULL)
  1628.                     {
  1629.                              // get new frame
  1630.                         newFrameP =    curSpriteP->frameArray[currentFrameIndex];
  1631.                         
  1632.                             // call it
  1633.                         (*curSpriteP->frameChangeProc)(curSpriteP, newFrameP,
  1634.                                 &curSpriteP->curFrameIndex);
  1635.                         
  1636.                             // make sure the new frame index is in range
  1637.                         if (curSpriteP->curFrameIndex < 0)
  1638.                         {
  1639.                             curSpriteP->curFrameIndex = 0;
  1640.                         }
  1641.                         else if (curSpriteP->curFrameIndex >= curSpriteP->maxFrames)
  1642.                         {
  1643.                             curSpriteP->curFrameIndex = curSpriteP->maxFrames - 1;
  1644.                         }
  1645.                     }
  1646.                     
  1647.                         // change the frame
  1648.                     newFrameP =    curSpriteP->frameArray[curSpriteP->curFrameIndex];
  1649.                     
  1650.                         // has the frame actually changed?
  1651.                     if (curSpriteP->curFrameP != newFrameP)
  1652.                     {
  1653.                         SWSetCurrentFrameIndex(curSpriteP, curSpriteP->curFrameIndex);
  1654.                     }
  1655.                 }
  1656.                 
  1657.                     // is it time to move the sprite?
  1658.                 if ( curSpriteP->moveTimeInterval >= 0 && 
  1659.                     (spriteWorldP->runningTimeCount - curSpriteP->timeOfLastMove) >=
  1660.                     curSpriteP->moveTimeInterval )
  1661.                 {
  1662.                     curSpriteP->timeOfLastMove = spriteWorldP->runningTimeCount;
  1663.                     
  1664.                         // is there a movement callback?
  1665.                     if (curSpriteP->spriteMoveProc != NULL)
  1666.                     {
  1667.                         (*curSpriteP->spriteMoveProc)(curSpriteP);
  1668.                     }
  1669.                 }
  1670.                 curSpriteP = nextSpriteP;
  1671.             }
  1672.         }
  1673.         curSpriteLayerP = curSpriteLayerP->nextSpriteLayerP;
  1674.     }
  1675.     
  1676.     gSWCurrentSpriteWorld = NULL;
  1677.  
  1678. }
  1679.  
  1680.  
  1681. ///--------------------------------------------------------------------------------------
  1682. //    SWProcessNonScrollingLayers - processes those layers in a scrolling world that need
  1683. //    to be moved after the visScrollRect is moved. Also ensures each Sprite's coordinate
  1684. //    system remains local to the window, not to the virtual world.
  1685. ///--------------------------------------------------------------------------------------
  1686.  
  1687. SW_FUNC void SWProcessNonScrollingLayers(
  1688.     SpriteWorldPtr spriteWorldP)
  1689. {
  1690.     register SpriteLayerPtr        curSpriteLayerP;
  1691.     
  1692.         // Exit if there's no work for us to do.
  1693.     if (spriteWorldP->thereAreNonScrollingLayers == false)
  1694.         return;
  1695.     
  1696.     curSpriteLayerP = spriteWorldP->headSpriteLayerP;    
  1697.     
  1698.         // iterate through the layers in this world
  1699.     while (curSpriteLayerP != NULL)
  1700.     {
  1701.             // Process any non-scrolling layers
  1702.         if (curSpriteLayerP->layerIsNonScrolling)
  1703.         {
  1704.             SWProcessSpriteLayer(spriteWorldP, curSpriteLayerP);
  1705.         }
  1706.             
  1707.         curSpriteLayerP = curSpriteLayerP->nextSpriteLayerP;
  1708.     }
  1709.     
  1710.         // We've finished processing all non-scrolling layers for this frame.
  1711.     spriteWorldP->thereAreNonScrollingLayers = false;
  1712. }
  1713.  
  1714.  
  1715. ///--------------------------------------------------------------------------------------
  1716. //    SWProcessSpriteLayer
  1717. ///--------------------------------------------------------------------------------------
  1718.  
  1719. SW_FUNC void SWProcessSpriteLayer(
  1720.     SpriteWorldPtr spriteWorldP,
  1721.     SpriteLayerPtr curSpriteLayerP)
  1722. {
  1723.     register SpritePtr            curSpriteP, nextSpriteP;
  1724.     register FramePtr            newFrameP;
  1725.     
  1726.     if (!curSpriteLayerP->isPaused)
  1727.     {
  1728.         curSpriteP = curSpriteLayerP->headSpriteP;
  1729.         
  1730.             // iterate through the sprites in this layer
  1731.         while (curSpriteP != NULL)
  1732.         {            
  1733.             SW_ASSERT(curSpriteP->curFrameP->isFrameLocked);
  1734.             nextSpriteP = curSpriteP->nextSpriteP;
  1735.             
  1736.                 // is it time to advance the sprite’s frame?
  1737.             if ( curSpriteP->frameTimeInterval >= 0 && 
  1738.                 (spriteWorldP->runningTimeCount - curSpriteP->timeOfLastFrameChange) >=
  1739.                 curSpriteP->frameTimeInterval )
  1740.             {
  1741.                 register long        currentFrameIndex;
  1742.                 
  1743.                 
  1744.                 curSpriteP->timeOfLastFrameChange = spriteWorldP->runningTimeCount;
  1745.                 
  1746.                 currentFrameIndex  = curSpriteP->curFrameIndex;
  1747.                 currentFrameIndex += curSpriteP->frameAdvance;
  1748.                 
  1749.                 if (currentFrameIndex < curSpriteP->firstFrameIndex)
  1750.                 {
  1751.                     if (curSpriteP->frameAdvanceMode == kSWWrapAroundMode)
  1752.                     {
  1753.                             // wrap to the last frame
  1754.                         currentFrameIndex = curSpriteP->lastFrameIndex;
  1755.                     }
  1756.                     else    // curSpriteP->frameAdvanceMode == kSWPatrollingMode
  1757.                     {
  1758.                             // change direction and set index to what it should be
  1759.                         curSpriteP->frameAdvance = -curSpriteP->frameAdvance;
  1760.                         currentFrameIndex += curSpriteP->frameAdvance;
  1761.                         currentFrameIndex += curSpriteP->frameAdvance;
  1762.                     }
  1763.                 }
  1764.                 else if (currentFrameIndex > curSpriteP->lastFrameIndex)
  1765.                 {
  1766.                     if (curSpriteP->frameAdvanceMode == kSWWrapAroundMode)
  1767.                     {
  1768.                             // wrap to the first frame
  1769.                         currentFrameIndex = curSpriteP->firstFrameIndex;
  1770.                     }
  1771.                     else    // curSpriteP->frameAdvanceMode == kSWPatrollingMode
  1772.                     {
  1773.                             // change direction and set index to what it should be
  1774.                         curSpriteP->frameAdvance = -curSpriteP->frameAdvance;
  1775.                         currentFrameIndex += curSpriteP->frameAdvance;
  1776.                         currentFrameIndex += curSpriteP->frameAdvance;
  1777.                     }
  1778.                 }
  1779.                 
  1780.                 curSpriteP->curFrameIndex = currentFrameIndex;
  1781.                 
  1782.                 
  1783.                     // is there a frame callback?
  1784.                 if (curSpriteP->frameChangeProc != NULL)
  1785.                 {
  1786.                          // get new frame
  1787.                     newFrameP =    curSpriteP->frameArray[currentFrameIndex];
  1788.                     
  1789.                         // call it
  1790.                     (*curSpriteP->frameChangeProc)(curSpriteP, newFrameP,
  1791.                             &curSpriteP->curFrameIndex);
  1792.                     
  1793.                         // make sure the new frame index is in range
  1794.                     if (curSpriteP->curFrameIndex < 0)
  1795.                     {
  1796.                         curSpriteP->curFrameIndex = 0;
  1797.                     }
  1798.                     else if (curSpriteP->curFrameIndex >= curSpriteP->maxFrames)
  1799.                     {
  1800.                         curSpriteP->curFrameIndex = curSpriteP->maxFrames - 1;
  1801.                     }
  1802.                 }
  1803.                 
  1804.                     // change the frame
  1805.                 newFrameP =    curSpriteP->frameArray[curSpriteP->curFrameIndex];
  1806.                 
  1807.                     // has the frame actually changed?
  1808.                 if (curSpriteP->curFrameP != newFrameP)
  1809.                 {
  1810.                     SWSetCurrentFrameIndex(curSpriteP, curSpriteP->curFrameIndex);
  1811.                 }
  1812.             }
  1813.             
  1814.                 // is it time to move the sprite?
  1815.             if ( curSpriteP->moveTimeInterval >= 0 && 
  1816.                 (spriteWorldP->runningTimeCount - curSpriteP->timeOfLastMove) >=
  1817.                 curSpriteP->moveTimeInterval )
  1818.             {
  1819.                 curSpriteP->timeOfLastMove = spriteWorldP->runningTimeCount;
  1820.                 
  1821.                     // is there a movement callback?
  1822.                 if (curSpriteP->spriteMoveProc != NULL)
  1823.                 {
  1824.                     (*curSpriteP->spriteMoveProc)(curSpriteP);
  1825.                 }
  1826.             }
  1827.             
  1828.             curSpriteP = nextSpriteP;
  1829.         }
  1830.     }
  1831. }
  1832.  
  1833.  
  1834. ///--------------------------------------------------------------------------------------
  1835. //    SWAnimateSpriteWorld
  1836. ///--------------------------------------------------------------------------------------
  1837.  
  1838. SW_FUNC void SWAnimateSpriteWorld(
  1839.     SpriteWorldPtr spriteWorldP)
  1840. {
  1841.     UpdateRectStructPtr            curRectStructP,
  1842.                                 nextRectStructP;
  1843.     register SpriteLayerPtr     curSpriteLayerP;
  1844.     register SpritePtr             curSpriteP;
  1845.     SpritePtr                     headActiveSpriteP = NULL;    // Tail of active sprite list
  1846.     SpritePtr                    headIdleSpriteP = NULL;        // Tail of idle sprite list
  1847.     SpritePtr                     curActiveSpriteP = NULL;
  1848.     SpritePtr                     curIdleSpriteP = NULL;
  1849.     Rect                        screenDestRect;
  1850.     Rect                        *changedRectP;
  1851.     short                        index, curTileLayer;
  1852.  
  1853.     SW_ASSERT(spriteWorldP != NULL);
  1854.     SW_ASSERT(spriteWorldP->backFrameP->isFrameLocked);
  1855.     SW_ASSERT(spriteWorldP->workFrameP->isFrameLocked);
  1856.     SW_ASSERT(spriteWorldP->windowFrameP->isFrameLocked);
  1857.     
  1858.     gSWCurrentSpriteWorld = spriteWorldP;
  1859.     
  1860.     if (!spriteWorldP->frameHasOccurred)
  1861.         return;
  1862.     
  1863.         // Add the deadSpriteLayer if there are any Sprites in it, so they get erased.
  1864.     if ( spriteWorldP->deadSpriteLayerP->headSpriteP != NULL )
  1865.     {
  1866.         SWAddSpriteLayer(spriteWorldP, spriteWorldP->deadSpriteLayerP);
  1867.     }
  1868.     
  1869.         // the current port should be the one in which we are drawing    
  1870.     SWSetPortToFrame( spriteWorldP->workFrameP );
  1871.     
  1872.     //-----------------erase the sprites--------------------
  1873.     
  1874.     curSpriteLayerP = spriteWorldP->headSpriteLayerP;
  1875.     curTileLayer = 0;
  1876.     
  1877.         // iterate through the layers in this world
  1878.     while (curSpriteLayerP != NULL)
  1879.     {
  1880.         curSpriteP = curSpriteLayerP->headSpriteP;
  1881.         
  1882.         if (curSpriteLayerP->tileLayer > curTileLayer)
  1883.             curTileLayer = curSpriteLayerP->tileLayer;
  1884.         
  1885.             // iterate through the sprites in this layer
  1886.         while (curSpriteP != NULL)
  1887.         {
  1888.             SW_ASSERT(curSpriteP->curFrameP->isFrameLocked);
  1889.             curSpriteP->tileDepth = curTileLayer;
  1890.             
  1891.             if ((curSpriteP->needsToBeDrawn && curSpriteP->isVisible) ||
  1892.                 (curSpriteP->needsToBeErased && !curSpriteP->isVisible))
  1893.             {
  1894.                     // Add sprite to active sprite list
  1895.                 if (headActiveSpriteP == NULL)
  1896.                     headActiveSpriteP = curSpriteP;
  1897.                 
  1898.                 if (curActiveSpriteP != NULL)
  1899.                         curActiveSpriteP->nextActiveSpriteP = curSpriteP;
  1900.                 
  1901.                 curActiveSpriteP = curSpriteP;
  1902.                 
  1903.                     // union last rect and current rect
  1904.                     // this way is much faster than UnionRect
  1905.                 curSpriteP->deltaFrameRect.top =
  1906.                     SW_MIN(curSpriteP->oldFrameRect.top, curSpriteP->destFrameRect.top);
  1907.                 curSpriteP->deltaFrameRect.left =
  1908.                     SW_MIN(curSpriteP->oldFrameRect.left, curSpriteP->destFrameRect.left);
  1909.                 curSpriteP->deltaFrameRect.bottom =
  1910.                     SW_MAX(curSpriteP->oldFrameRect.bottom, curSpriteP->destFrameRect.bottom);
  1911.                 curSpriteP->deltaFrameRect.right =
  1912.                     SW_MAX(curSpriteP->oldFrameRect.right, curSpriteP->destFrameRect.right);
  1913.                 {
  1914.                     short temp;
  1915.                     
  1916.                         // align the left edge to long word boundary
  1917.                     curSpriteP->deltaFrameRect.left &=
  1918.                             (spriteWorldP->workFrameP->leftAlignFactor);
  1919.                     
  1920.                         // align the right edge to long word boundary
  1921.                     temp = curSpriteP->deltaFrameRect.right &
  1922.                         spriteWorldP->workFrameP->rightAlignFactor;
  1923.                     if (temp != 0)
  1924.                     {
  1925.                         curSpriteP->deltaFrameRect.right +=
  1926.                                 (spriteWorldP->workFrameP->rightAlignFactor + 1) - temp;
  1927.                     }
  1928.                     
  1929.                         // align the left edge to long word boundary
  1930.                     curSpriteP->oldFrameRect.left &=
  1931.                         (spriteWorldP->workFrameP->leftAlignFactor);
  1932.                     
  1933.                         // align the right edge to long word boundary
  1934.                     temp = curSpriteP->oldFrameRect.right &
  1935.                         spriteWorldP->workFrameP->rightAlignFactor;
  1936.                     if (temp != 0)
  1937.                     {
  1938.                         curSpriteP->oldFrameRect.right +=
  1939.                             (spriteWorldP->workFrameP->rightAlignFactor + 1) - temp;
  1940.                     }
  1941.                 }
  1942.                 
  1943.                     // copy the back drop piece
  1944.                 (*spriteWorldP->offscreenDrawProc)(
  1945.                         spriteWorldP->backFrameP,
  1946.                         spriteWorldP->workFrameP,
  1947.                         &curSpriteP->oldFrameRect,
  1948.                         &curSpriteP->oldFrameRect);
  1949.             }
  1950.             else if (curSpriteP->isVisible)
  1951.             {
  1952.                     // Add sprite to idle sprite list
  1953.                 if (headIdleSpriteP == NULL)
  1954.                     headIdleSpriteP = curSpriteP;
  1955.                 
  1956.                 if (curIdleSpriteP != NULL)
  1957.                     curIdleSpriteP->nextIdleSpriteP = curSpriteP;
  1958.                 
  1959.                 curIdleSpriteP = curSpriteP;
  1960.             }
  1961.             
  1962.             curSpriteP = curSpriteP->nextSpriteP;
  1963.         }
  1964.         
  1965.         curSpriteLayerP = curSpriteLayerP->nextSpriteLayerP;    
  1966.     }
  1967.     
  1968.     if (curActiveSpriteP != NULL)
  1969.         curActiveSpriteP->nextActiveSpriteP = NULL;
  1970.     
  1971.     if (curIdleSpriteP != NULL)
  1972.         curIdleSpriteP->nextIdleSpriteP = NULL;
  1973.     
  1974.         // update flagged background rects
  1975.     curRectStructP = spriteWorldP->headUpdateRectP;
  1976.     while ( curRectStructP != NULL )
  1977.     {
  1978.         (*spriteWorldP->offscreenDrawProc)(
  1979.                         spriteWorldP->backFrameP,
  1980.                         spriteWorldP->workFrameP,
  1981.                         &curRectStructP->updateRect,
  1982.                         &curRectStructP->updateRect);
  1983.         curRectStructP = curRectStructP->nextRectStructP;
  1984.     }
  1985.     
  1986.         // Redraw idle sprites that were erased by a tile
  1987.     if (spriteWorldP->numTilesChanged > 0)
  1988.         SWCheckIdleSpritesWithTiles(spriteWorldP, headIdleSpriteP);
  1989.  
  1990.         // Redraw idle sprites that were erased by an updateRect
  1991.     if (spriteWorldP->headUpdateRectP != NULL)
  1992.         SWCheckIdleSpritesWithRects(spriteWorldP, headIdleSpriteP);
  1993.  
  1994.         // Call the postEraseCallBack
  1995.     if (spriteWorldP->postEraseCallBack != NULL)
  1996.         (*spriteWorldP->postEraseCallBack)(spriteWorldP);
  1997.     
  1998.     
  1999.     //-----------------draw the sprites--------------------
  2000.     
  2001.     curSpriteLayerP = spriteWorldP->headSpriteLayerP;
  2002.     
  2003.         // iterate through the layers in this world
  2004.     while (curSpriteLayerP != NULL)
  2005.     {
  2006.         curSpriteP = curSpriteLayerP->headSpriteP;
  2007.         
  2008.             // iterate through the sprites in this layer
  2009.         while (curSpriteP != NULL)
  2010.         {
  2011.             if (curSpriteP->isVisible)
  2012.             {
  2013.                 if (curSpriteP->needsToBeDrawn)
  2014.                 {
  2015.                     gSWCurrentSpriteBeingDrawn = curSpriteP;
  2016.                     
  2017.                         // draw the sprite in the work area
  2018.                     (*curSpriteP->frameDrawProc)(
  2019.                             curSpriteP->curFrameP,
  2020.                             spriteWorldP->workFrameP,
  2021.                             &curSpriteP->curFrameP->frameRect,
  2022.                             &curSpriteP->destFrameRect);
  2023.                             
  2024.                     gSWCurrentSpriteBeingDrawn = NULL;
  2025.                     
  2026.                     if (spriteWorldP->tilingIsOn &&
  2027.                         curSpriteP->tileDepth <= spriteWorldP->lastActiveTileLayer)
  2028.                     {
  2029.                         screenDestRect = curSpriteP->destFrameRect;
  2030.                         
  2031.                             // Clip screenDestRect to visScrollRect
  2032.                         if (screenDestRect.left < spriteWorldP->visScrollRect.left)
  2033.                             screenDestRect.left = spriteWorldP->visScrollRect.left;
  2034.                         if (screenDestRect.right > spriteWorldP->visScrollRect.right)
  2035.                             screenDestRect.right = spriteWorldP->visScrollRect.right;
  2036.                         if (screenDestRect.top < spriteWorldP->visScrollRect.top)
  2037.                             screenDestRect.top = spriteWorldP->visScrollRect.top;
  2038.                         if (screenDestRect.bottom > spriteWorldP->visScrollRect.bottom)
  2039.                             screenDestRect.bottom = spriteWorldP->visScrollRect.bottom;
  2040.         
  2041.                         SWDrawTilesAboveSprite(spriteWorldP, &screenDestRect, curSpriteP->tileDepth);
  2042.                     }
  2043.                 }
  2044.                 else
  2045.                 {
  2046.                     SWCheckIdleSpriteOverlap(spriteWorldP, curSpriteP, headActiveSpriteP);
  2047.                 }
  2048.             }
  2049.             
  2050.             curSpriteP = curSpriteP->nextSpriteP;
  2051.         }
  2052.         
  2053.         curSpriteLayerP = curSpriteLayerP->nextSpriteLayerP;
  2054.     }
  2055.     
  2056.     
  2057.          // Call the postDrawCallBack
  2058.     if (spriteWorldP->postDrawCallBack != NULL)
  2059.         (*spriteWorldP->postDrawCallBack)(spriteWorldP);
  2060.     
  2061.     
  2062.     //-----------------update the screen--------------------
  2063.     
  2064.         // the current port should be the one in which we are drawing    
  2065.     SWSetPortToFrame( spriteWorldP->windowFrameP );
  2066.     
  2067.         // wait for the VBL
  2068.     if ( spriteWorldP->usingVBL )
  2069.     {  
  2070.         spriteWorldP->vblTaskRec.hasVBLFired = false;
  2071.         while ( !spriteWorldP->vblTaskRec.hasVBLFired )
  2072.         {}
  2073.     }
  2074.     
  2075.     curSpriteP = headActiveSpriteP;
  2076.     
  2077.         // Has a custom worldRect been used? (If so, sprites need offsetting)
  2078.     if (spriteWorldP->windRect.left || spriteWorldP->windRect.top)
  2079.     {
  2080.             // update flagged background rects
  2081.         curRectStructP = spriteWorldP->headUpdateRectP;
  2082.         while ( curRectStructP != NULL )
  2083.         {
  2084.             screenDestRect = curRectStructP->updateRect;
  2085.             screenDestRect.left += spriteWorldP->windRect.left;
  2086.             screenDestRect.right += spriteWorldP->windRect.left;
  2087.             screenDestRect.top += spriteWorldP->windRect.top;
  2088.             screenDestRect.bottom += spriteWorldP->windRect.top;
  2089.             (*spriteWorldP->screenDrawProc)(
  2090.                             spriteWorldP->workFrameP,
  2091.                             spriteWorldP->windowFrameP,
  2092.                             &curRectStructP->updateRect,
  2093.                             &screenDestRect);                        
  2094.             curRectStructP = curRectStructP->nextRectStructP;
  2095.         }
  2096.         
  2097.             // Update on screen the tiles that have changed
  2098.         changedRectP = spriteWorldP->changedTiles;
  2099.         for (index = 0; index < spriteWorldP->numTilesChanged; index++, changedRectP++)
  2100.         {
  2101.             screenDestRect = *changedRectP;
  2102.             
  2103.                 // offset dest rect for screen
  2104.             screenDestRect.left += spriteWorldP->windRect.left;
  2105.             screenDestRect.right += spriteWorldP->windRect.left;
  2106.             screenDestRect.top += spriteWorldP->windRect.top;
  2107.             screenDestRect.bottom += spriteWorldP->windRect.top;
  2108.             
  2109.             (*spriteWorldP->screenDrawProc)(spriteWorldP->workFrameP,
  2110.                     spriteWorldP->windowFrameP, changedRectP, &screenDestRect);
  2111.         }
  2112.         
  2113.         
  2114.             // update the sprites on the screen
  2115.         while (curSpriteP != NULL)
  2116.         {    
  2117.                 // offset the sprite's delta rect for the screen
  2118.             screenDestRect = curSpriteP->deltaFrameRect;
  2119.             
  2120.             screenDestRect.left += spriteWorldP->windRect.left;
  2121.             screenDestRect.right += spriteWorldP->windRect.left;
  2122.             screenDestRect.top += spriteWorldP->windRect.top;
  2123.             screenDestRect.bottom += spriteWorldP->windRect.top;
  2124.             
  2125.             (*spriteWorldP->screenDrawProc)(
  2126.                     spriteWorldP->workFrameP,
  2127.                     spriteWorldP->windowFrameP,
  2128.                     &curSpriteP->deltaFrameRect,
  2129.                     &screenDestRect);
  2130.             
  2131.             
  2132.                 // set the delta and last rect to the current rect
  2133.             curSpriteP->deltaFrameRect = curSpriteP->destFrameRect;
  2134.             curSpriteP->oldFrameRect = curSpriteP->destFrameRect;
  2135.             
  2136.                 // this sprite no longer needs to be drawn
  2137.             curSpriteP->needsToBeDrawn = false;
  2138.             curSpriteP->needsToBeErased = false;
  2139.             
  2140.             curSpriteP = curSpriteP->nextActiveSpriteP;
  2141.         }
  2142.     }
  2143.     else
  2144.     {
  2145.             // update flagged background rects
  2146.         curRectStructP = spriteWorldP->headUpdateRectP;
  2147.         while ( curRectStructP != NULL )
  2148.         {
  2149.             (*spriteWorldP->screenDrawProc)(
  2150.                             spriteWorldP->workFrameP,
  2151.                             spriteWorldP->windowFrameP,
  2152.                             &curRectStructP->updateRect,
  2153.                             &curRectStructP->updateRect);                        
  2154.             curRectStructP = curRectStructP->nextRectStructP;
  2155.         }
  2156.         
  2157.             // Update on screen the tiles that have changed
  2158.         changedRectP = spriteWorldP->changedTiles;
  2159.         for (index = 0; index < spriteWorldP->numTilesChanged; index++, changedRectP++)
  2160.         {
  2161.             (*spriteWorldP->screenDrawProc)(spriteWorldP->workFrameP,
  2162.                     spriteWorldP->windowFrameP, changedRectP, changedRectP);
  2163.         }
  2164.     
  2165.         
  2166.             // update the sprites on the screen
  2167.         while (curSpriteP != NULL)
  2168.         {    
  2169.             (*spriteWorldP->screenDrawProc)(
  2170.                     spriteWorldP->workFrameP,
  2171.                     spriteWorldP->windowFrameP,
  2172.                     &curSpriteP->deltaFrameRect,
  2173.                     &curSpriteP->deltaFrameRect);
  2174.             
  2175.             
  2176.                 // set the delta and last rect to the current rect
  2177.             curSpriteP->deltaFrameRect = curSpriteP->destFrameRect;
  2178.             curSpriteP->oldFrameRect = curSpriteP->destFrameRect;
  2179.             
  2180.                 // this sprite no longer needs to be drawn
  2181.             curSpriteP->needsToBeDrawn = false;
  2182.             curSpriteP->needsToBeErased = false;
  2183.             
  2184.             curSpriteP = curSpriteP->nextActiveSpriteP;
  2185.         }
  2186.     }
  2187.     
  2188.         // dispose of flagged background rects
  2189.     nextRectStructP = spriteWorldP->headUpdateRectP;
  2190.     while ( nextRectStructP != NULL )
  2191.     {
  2192.         curRectStructP = nextRectStructP;
  2193.         nextRectStructP = curRectStructP->nextRectStructP;
  2194.         DisposePtr( (Ptr)curRectStructP );
  2195.     }
  2196.     spriteWorldP->headUpdateRectP = NULL;
  2197.     
  2198.     spriteWorldP->numTilesChanged = 0;
  2199.     
  2200.         // Remove the deadSpriteLayer if we added it earlier.
  2201.     if ( spriteWorldP->deadSpriteLayerP->headSpriteP != NULL )
  2202.     {
  2203.         SWRemoveSpriteLayer(spriteWorldP, spriteWorldP->deadSpriteLayerP);
  2204.     }
  2205.     
  2206.     gSWCurrentSpriteWorld = NULL;
  2207.  
  2208. }
  2209.  
  2210.     
  2211. ///--------------------------------------------------------------------------------------
  2212. //    SWCheckIdleSpriteOverlap
  2213. ///--------------------------------------------------------------------------------------
  2214.  
  2215. SW_FUNC void SWCheckIdleSpriteOverlap(
  2216.     SpriteWorldPtr    spriteWorldP,
  2217.     SpritePtr        idleSpriteP,
  2218.     SpritePtr        headActiveSpriteP)
  2219. {
  2220.     register SpritePtr         activeSpriteP = headActiveSpriteP;
  2221.     Rect                    srcSectRect, dstSectRect;
  2222.     
  2223.         // iterate through the active sprites
  2224.     while (activeSpriteP != NULL)
  2225.     {
  2226.             // do the sprites overlap?
  2227.         if ((idleSpriteP->oldFrameRect.top < activeSpriteP->deltaFrameRect.bottom) &&
  2228.              (idleSpriteP->oldFrameRect.bottom > activeSpriteP->deltaFrameRect.top) &&
  2229.              (idleSpriteP->oldFrameRect.left < activeSpriteP->deltaFrameRect.right) &&
  2230.              (idleSpriteP->oldFrameRect.right > activeSpriteP->deltaFrameRect.left))
  2231.         {
  2232.                 // calculate the intersection between the idle sprite's destination
  2233.                 // rect, and the active sprite's delta rect
  2234.             dstSectRect.left =
  2235.                     SW_MAX(idleSpriteP->destFrameRect.left, activeSpriteP->deltaFrameRect.left);
  2236.             dstSectRect.top =
  2237.                     SW_MAX(idleSpriteP->destFrameRect.top, activeSpriteP->deltaFrameRect.top);
  2238.             dstSectRect.right =
  2239.                     SW_MIN(idleSpriteP->destFrameRect.right, activeSpriteP->deltaFrameRect.right);
  2240.             dstSectRect.bottom =
  2241.                     SW_MIN(idleSpriteP->destFrameRect.bottom, activeSpriteP->deltaFrameRect.bottom);
  2242.             
  2243.             srcSectRect = idleSpriteP->curFrameP->frameRect;
  2244.             
  2245.             srcSectRect.left += (dstSectRect.left - idleSpriteP->destFrameRect.left);
  2246.             srcSectRect.top += (dstSectRect.top - idleSpriteP->destFrameRect.top);
  2247.             srcSectRect.right -= (idleSpriteP->destFrameRect.right - dstSectRect.right);
  2248.             srcSectRect.bottom -= (idleSpriteP->destFrameRect.bottom - dstSectRect.bottom);
  2249.             
  2250.             
  2251.                 // copy a piece of the sprite image onto the workFrame    
  2252.                 
  2253.             gSWCurrentSpriteBeingDrawn = idleSpriteP;
  2254.             
  2255.             (*idleSpriteP->frameDrawProc)(
  2256.                     idleSpriteP->curFrameP,
  2257.                     spriteWorldP->workFrameP,
  2258.                     &srcSectRect,
  2259.                     &dstSectRect);
  2260.  
  2261.             
  2262.             gSWCurrentSpriteBeingDrawn = NULL;
  2263.             
  2264.             if (spriteWorldP->tilingIsOn &&
  2265.                 idleSpriteP->tileDepth <= spriteWorldP->lastActiveTileLayer)
  2266.             {
  2267.                     // Clip dstSectRect to visScrollRect
  2268.                 if (dstSectRect.left < spriteWorldP->visScrollRect.left)
  2269.                     dstSectRect.left = spriteWorldP->visScrollRect.left;
  2270.                 if (dstSectRect.right > spriteWorldP->visScrollRect.right)
  2271.                     dstSectRect.right = spriteWorldP->visScrollRect.right;
  2272.                 if (dstSectRect.top < spriteWorldP->visScrollRect.top)
  2273.                     dstSectRect.top = spriteWorldP->visScrollRect.top;
  2274.                 if (dstSectRect.bottom > spriteWorldP->visScrollRect.bottom)
  2275.                     dstSectRect.bottom = spriteWorldP->visScrollRect.bottom;
  2276.  
  2277.                 SWDrawTilesAboveSprite(spriteWorldP, &dstSectRect, idleSpriteP->tileDepth);
  2278.             }
  2279.         }
  2280.         
  2281.         activeSpriteP = activeSpriteP->nextActiveSpriteP;
  2282.     }
  2283. }
  2284.  
  2285.  
  2286. ///--------------------------------------------------------------------------------------
  2287. //    SWCheckIdleSpritesWithTiles - redraw sprites erased by tiles that changed
  2288. ///--------------------------------------------------------------------------------------
  2289.  
  2290. SW_FUNC void SWCheckIdleSpritesWithTiles(
  2291.     SpriteWorldPtr    spriteWorldP,
  2292.     SpritePtr        headIdleSpriteP)
  2293. {
  2294.     Rect        srcSectRect, dstSectRect;
  2295.     register    SpritePtr idleSpriteP;
  2296.     short        srcHorizOffset;
  2297.     short        srcVertOffset;
  2298.     Rect        *changedRectP;
  2299.     short        index;
  2300.     
  2301.  
  2302.         // Cycle through the changedTiles array of rects
  2303.     changedRectP = spriteWorldP->changedTiles;
  2304.     for (index = 0; index < spriteWorldP->numTilesChanged; index++, changedRectP++)
  2305.     {
  2306.         idleSpriteP = headIdleSpriteP;
  2307.     
  2308.             // iterate through the idle sprites
  2309.         while (idleSpriteP != NULL)
  2310.         {
  2311.                 // does the idle sprite overlap the changedRect?
  2312.             if ((idleSpriteP->oldFrameRect.top < changedRectP->bottom) &&
  2313.                  (idleSpriteP->oldFrameRect.bottom > changedRectP->top) &&
  2314.                  (idleSpriteP->oldFrameRect.left < changedRectP->right) &&
  2315.                  (idleSpriteP->oldFrameRect.right > changedRectP->left))
  2316.             {
  2317.                     // calculate the intersection between the idle
  2318.                     // sprite's rect and the changedRectP
  2319.                 dstSectRect.left = 
  2320.                         SW_MAX(idleSpriteP->oldFrameRect.left, changedRectP->left);
  2321.                 dstSectRect.top = 
  2322.                         SW_MAX(idleSpriteP->oldFrameRect.top, changedRectP->top);
  2323.                 dstSectRect.right = 
  2324.                         SW_MIN(idleSpriteP->oldFrameRect.right, changedRectP->right);
  2325.                 dstSectRect.bottom = 
  2326.                         SW_MIN(idleSpriteP->oldFrameRect.bottom, changedRectP->bottom);
  2327.                     
  2328.                     // Calculate the source rect
  2329.                 srcSectRect = dstSectRect;
  2330.                 
  2331.                 srcHorizOffset = idleSpriteP->curFrameP->frameRect.left;
  2332.                 srcVertOffset = idleSpriteP->curFrameP->frameRect.top;
  2333.                 
  2334.                 srcSectRect.left -= (idleSpriteP->oldFrameRect.left - srcHorizOffset);
  2335.                 srcSectRect.right -= (idleSpriteP->oldFrameRect.left - srcHorizOffset);
  2336.                 srcSectRect.top -= (idleSpriteP->oldFrameRect.top - srcVertOffset);
  2337.                 srcSectRect.bottom -= (idleSpriteP->oldFrameRect.top  - srcVertOffset);
  2338.     
  2339.                     // Copy a piece of the sprite image onto the back drop piece
  2340.                 
  2341.                 gSWCurrentSpriteBeingDrawn = idleSpriteP;    
  2342.                 
  2343.                 (*idleSpriteP->frameDrawProc)(
  2344.                         idleSpriteP->curFrameP,
  2345.                         spriteWorldP->workFrameP,
  2346.                         &srcSectRect,
  2347.                         &dstSectRect);
  2348.                 
  2349.                 gSWCurrentSpriteBeingDrawn = NULL;
  2350.                 
  2351.                 if (spriteWorldP->tilingIsOn &&
  2352.                     idleSpriteP->tileDepth <= spriteWorldP->lastActiveTileLayer)
  2353.                 {
  2354.                     dstSectRect.top += spriteWorldP->vertScrollRectOffset;
  2355.                     dstSectRect.bottom += spriteWorldP->vertScrollRectOffset;
  2356.                     dstSectRect.left += spriteWorldP->horizScrollRectOffset;
  2357.                     dstSectRect.right += spriteWorldP->horizScrollRectOffset;
  2358.                     SWDrawTilesAboveSprite(spriteWorldP, &dstSectRect, idleSpriteP->tileDepth);
  2359.                 }
  2360.             }
  2361.             
  2362.             idleSpriteP = idleSpriteP->nextIdleSpriteP;
  2363.         }
  2364.     }
  2365. }
  2366.  
  2367.  
  2368. ///--------------------------------------------------------------------------------------
  2369. //    SWCheckIdleSpritesWithRects - redraw sprites erased by updateRects
  2370. ///--------------------------------------------------------------------------------------
  2371.  
  2372. SW_FUNC void SWCheckIdleSpritesWithRects(
  2373.     SpriteWorldPtr    spriteWorldP,
  2374.     SpritePtr        headIdleSpriteP)
  2375. {
  2376.     UpdateRectStructPtr    curRectStructP;
  2377.     register SpritePtr    idleSpriteP;
  2378.     Rect                srcSectRect, dstSectRect;
  2379.     short                srcHorizOffset;
  2380.     short                srcVertOffset;
  2381.     Rect                *changedRectP;
  2382.     
  2383.     
  2384.     curRectStructP = spriteWorldP->headUpdateRectP;
  2385.     
  2386.     while (curRectStructP != NULL)
  2387.     {
  2388.         changedRectP = &curRectStructP->updateRect;
  2389.         idleSpriteP = headIdleSpriteP;
  2390.     
  2391.             // iterate through the idle sprites
  2392.         while (idleSpriteP != NULL)
  2393.         {
  2394.                 // does the idle sprite overlap the changedRect?
  2395.             if ((idleSpriteP->oldFrameRect.top < changedRectP->bottom) &&
  2396.                  (idleSpriteP->oldFrameRect.bottom > changedRectP->top) &&
  2397.                  (idleSpriteP->oldFrameRect.left < changedRectP->right) &&
  2398.                  (idleSpriteP->oldFrameRect.right > changedRectP->left))
  2399.             {
  2400.                     // calculate the intersection between the idle
  2401.                     // sprite's rect and the changedRectP
  2402.                 dstSectRect.left = 
  2403.                     SW_MAX(idleSpriteP->oldFrameRect.left, changedRectP->left);
  2404.                 dstSectRect.top = 
  2405.                     SW_MAX(idleSpriteP->oldFrameRect.top, changedRectP->top);
  2406.                 dstSectRect.right = 
  2407.                     SW_MIN(idleSpriteP->oldFrameRect.right, changedRectP->right);
  2408.                 dstSectRect.bottom = 
  2409.                     SW_MIN(idleSpriteP->oldFrameRect.bottom, changedRectP->bottom);
  2410.                     
  2411.                     // Calculate the source rect
  2412.                 srcSectRect = dstSectRect;
  2413.                 
  2414.                 srcHorizOffset = idleSpriteP->curFrameP->frameRect.left;
  2415.                 srcVertOffset = idleSpriteP->curFrameP->frameRect.top;
  2416.                 
  2417.                 srcSectRect.left -= (idleSpriteP->oldFrameRect.left - srcHorizOffset);
  2418.                 srcSectRect.right -= (idleSpriteP->oldFrameRect.left - srcHorizOffset);
  2419.                 srcSectRect.top -= (idleSpriteP->oldFrameRect.top - srcVertOffset);
  2420.                 srcSectRect.bottom -= (idleSpriteP->oldFrameRect.top  - srcVertOffset);
  2421.     
  2422.                     // Copy a piece of the sprite image onto the back drop piece    
  2423.                 
  2424.                 gSWCurrentSpriteBeingDrawn = idleSpriteP;
  2425.  
  2426.                 (*idleSpriteP->frameDrawProc)(
  2427.                         idleSpriteP->curFrameP,
  2428.                         spriteWorldP->workFrameP,
  2429.                         &srcSectRect,
  2430.                         &dstSectRect);
  2431.                 
  2432.                 gSWCurrentSpriteBeingDrawn = NULL;
  2433.                 
  2434.                 if (spriteWorldP->tilingIsOn &&
  2435.                     idleSpriteP->tileDepth <= spriteWorldP->lastActiveTileLayer)
  2436.                 {
  2437.                     dstSectRect.top += spriteWorldP->vertScrollRectOffset;
  2438.                     dstSectRect.bottom += spriteWorldP->vertScrollRectOffset;
  2439.                     dstSectRect.left += spriteWorldP->horizScrollRectOffset;
  2440.                     dstSectRect.right += spriteWorldP->horizScrollRectOffset;
  2441.                     SWDrawTilesAboveSprite(spriteWorldP, &dstSectRect, idleSpriteP->tileDepth);
  2442.                 }
  2443.             }
  2444.             
  2445.             idleSpriteP = idleSpriteP->nextIdleSpriteP;
  2446.         }
  2447.         
  2448.         curRectStructP = curRectStructP->nextRectStructP;
  2449.     }
  2450. }
  2451.  
  2452.  
  2453. ///--------------------------------------------------------------------------------------
  2454. //    SWFindSpritesToBeRemoved
  2455. ///--------------------------------------------------------------------------------------
  2456.  
  2457. SW_FUNC void SWFindSpritesToBeRemoved(
  2458.     SpriteWorldPtr spriteWorldP )
  2459. {
  2460.     SpritePtr        curSpriteP, nextSpriteP;
  2461.     
  2462.     curSpriteP = spriteWorldP->deadSpriteLayerP->headSpriteP;
  2463.         
  2464.         // iterate through the sprites in the deadSpriteLayerP
  2465.     while (curSpriteP != NULL)
  2466.     {    
  2467.         nextSpriteP = curSpriteP->nextSpriteP;
  2468.         
  2469.             // all the Sprites here should need removing...
  2470.         SW_ASSERT(curSpriteP->spriteRemoval != kSWDontRemoveSprite);
  2471.         
  2472.             // has this sprite been erased by SWAnimate?
  2473.         if (curSpriteP->needsToBeErased == false)
  2474.         {
  2475.                 // if so, we can safely remove it from its layer
  2476.             SWRemoveSprite(curSpriteP);
  2477.             
  2478.                 // do we want to dispose of this sprite too?
  2479.             if (curSpriteP->spriteRemoval == kSWRemoveAndDisposeSprite)
  2480.                 SWDisposeSprite(&curSpriteP);
  2481.             else
  2482.                 curSpriteP->spriteRemoval = kSWDontRemoveSprite;    // We're done
  2483.         }
  2484.         
  2485.         curSpriteP = nextSpriteP;
  2486.     }
  2487. }
  2488.  
  2489.  
  2490. ///--------------------------------------------------------------------------------------
  2491. //    SWFlagRectAsChanged
  2492. ///--------------------------------------------------------------------------------------
  2493.  
  2494. SW_FUNC OSErr SWFlagRectAsChanged(
  2495.     SpriteWorldPtr spriteWorldP,
  2496.     Rect*    theChangedRect)
  2497. {
  2498.     UpdateRectStructPtr        curRectStructP,
  2499.                             newUpdateRectP;
  2500.     short                    temp;
  2501.     OSErr                    err = noErr;
  2502.     
  2503.         // align the left edge to long word boundary
  2504.     theChangedRect->left &= (spriteWorldP->workFrameP->leftAlignFactor);
  2505.     
  2506.         // align the right edge to long word boundary
  2507.     temp = theChangedRect->right & spriteWorldP->workFrameP->rightAlignFactor;
  2508.     if (temp != 0)
  2509.     {
  2510.         theChangedRect->right += (spriteWorldP->workFrameP->rightAlignFactor + 1) - temp;
  2511.     }
  2512.                     
  2513.     newUpdateRectP = (UpdateRectStructPtr)NewPtr(sizeof(UpdateRectStruct));
  2514.     if ( newUpdateRectP != NULL )
  2515.     {
  2516.         newUpdateRectP->updateRect = *theChangedRect;
  2517.         newUpdateRectP->nextRectStructP = NULL;
  2518.         
  2519.         if ( spriteWorldP->headUpdateRectP == NULL )
  2520.         {
  2521.             spriteWorldP->headUpdateRectP = newUpdateRectP;
  2522.         }
  2523.         else
  2524.         {
  2525.             curRectStructP = spriteWorldP->headUpdateRectP;
  2526.             while ( curRectStructP->nextRectStructP != NULL )
  2527.             {
  2528.                 curRectStructP = curRectStructP->nextRectStructP;
  2529.             }
  2530.             curRectStructP->nextRectStructP = newUpdateRectP;
  2531.         }
  2532.     }
  2533.     else
  2534.     {
  2535.         err = MemError();
  2536.     }
  2537.     
  2538.     return err;
  2539. }
  2540.  
  2541.  
  2542. ///--------------------------------------------------------------------------------------
  2543. //    SWFlagScrollingRectAsChanged - same as SWFlagRectAsChanged, minus alignment code
  2544. ///--------------------------------------------------------------------------------------
  2545.  
  2546. SW_FUNC OSErr SWFlagScrollingRectAsChanged(
  2547.     SpriteWorldPtr spriteWorldP,
  2548.     Rect*    theChangedRect)
  2549. {
  2550.     UpdateRectStructPtr        curRectStructP,
  2551.                             newUpdateRectP;
  2552.     OSErr                    err = noErr;
  2553.     
  2554.                     
  2555.     newUpdateRectP = (UpdateRectStructPtr)NewPtr(sizeof(UpdateRectStruct));
  2556.     if ( newUpdateRectP != NULL )
  2557.     {
  2558.         newUpdateRectP->updateRect = *theChangedRect;
  2559.         newUpdateRectP->nextRectStructP = NULL;
  2560.         
  2561.         if ( spriteWorldP->headUpdateRectP == NULL )
  2562.         {
  2563.             spriteWorldP->headUpdateRectP = newUpdateRectP;
  2564.         }
  2565.         else
  2566.         {
  2567.             curRectStructP = spriteWorldP->headUpdateRectP;
  2568.             while ( curRectStructP->nextRectStructP != NULL )
  2569.             {
  2570.                 curRectStructP = curRectStructP->nextRectStructP;
  2571.             }
  2572.             curRectStructP->nextRectStructP = newUpdateRectP;
  2573.         }
  2574.     }
  2575.     else
  2576.     {
  2577.         err = MemError();
  2578.     }
  2579.     
  2580.     return err;
  2581. }
  2582.  
  2583.  
  2584. ///--------------------------------------------------------------------------------------
  2585. // SWSyncSpriteWorldToVBL
  2586. ///--------------------------------------------------------------------------------------
  2587.  
  2588. SW_FUNC OSErr SWSyncSpriteWorldToVBL(
  2589.     SpriteWorldPtr    spriteWorldP,
  2590.     Boolean            syncingOn)
  2591. {
  2592.     OSErr            err = noErr;
  2593.     AuxDCEHandle    mainDCE;
  2594.     
  2595.     SW_ASSERT(spriteWorldP != NULL);        
  2596.     
  2597.     if ( syncingOn )
  2598.     {
  2599.         if ( !spriteWorldP->usingVBL )
  2600.         {    
  2601.             *((VBLUPP*)&spriteWorldP->vblTaskRec.myVBLTask.vblAddr) =
  2602.                 NewVBLProc((ProcPtr)SWVBLTask);
  2603.             spriteWorldP->vblTaskRec.myVBLTask.qType = vType;
  2604.             spriteWorldP->vblTaskRec.myVBLTask.vblCount = 1;
  2605.             spriteWorldP->vblTaskRec.hasVBLFired = false;
  2606.             
  2607.                 // insert the task into the VBL queue
  2608.             mainDCE = (AuxDCEHandle)GetDCtlEntry((**spriteWorldP->mainSWGDH).gdRefNum);
  2609.             err = SlotVInstall( (QElemPtr) &spriteWorldP->vblTaskRec.myVBLTask, 
  2610.                     (**mainDCE).dCtlSlot );
  2611.             if ( err == noErr )
  2612.             {
  2613.                 spriteWorldP->usingVBL = true;
  2614.             }
  2615.         }
  2616.     }
  2617.     else
  2618.     {
  2619.         if ( spriteWorldP->usingVBL )
  2620.         {
  2621.             mainDCE = (AuxDCEHandle)GetDCtlEntry((**spriteWorldP->mainSWGDH).gdRefNum);
  2622.             err = SlotVRemove( (QElemPtr) &spriteWorldP->vblTaskRec.myVBLTask, 
  2623.                     (**mainDCE).dCtlSlot );
  2624.             spriteWorldP->vblTaskRec.hasVBLFired = true;
  2625.             spriteWorldP->usingVBL = false;
  2626.             DisposeRoutineDescriptor(spriteWorldP->vblTaskRec.myVBLTask.vblAddr);
  2627.         }
  2628.     }
  2629.  
  2630.     SWSetStickyIfError( err );
  2631.     return err;
  2632. }
  2633.  
  2634.  
  2635. ///--------------------------------------------------------------------------------------
  2636. // SWVBLTask
  2637. ///--------------------------------------------------------------------------------------
  2638.  
  2639. #if !GENERATINGCFM
  2640. extern    VBLTaskRecPtr GetVBLRec(void)
  2641.     = 0x2008;    /* MOVE.L    A0,D0 */
  2642. #endif
  2643.  
  2644. #if GENERATINGCFM
  2645. void SWVBLTask(VBLTaskRecPtr vblTaskPtr)
  2646. {
  2647.     vblTaskPtr->hasVBLFired = true;
  2648.     
  2649.         // Reset vblCount so that this procedure executes again
  2650.     vblTaskPtr->myVBLTask.vblCount = 1;
  2651. }
  2652. #else
  2653. void SWVBLTask( void )
  2654. {
  2655.     VBLTaskRecPtr vblTaskPtr;
  2656.     
  2657.     vblTaskPtr = (VBLTaskRecPtr) GetVBLRec();
  2658.     vblTaskPtr->hasVBLFired = true;
  2659.     
  2660.         // Reset vblCount so that this procedure executes again
  2661.     vblTaskPtr->myVBLTask.vblCount = 1;
  2662. }
  2663. #endif
  2664.  
  2665.  
  2666. ///--------------------------------------------------------------------------------------
  2667. //    SWSetCleanUpSpriteWorld - call this if you want FatalError() or SWAssertFail() to call 
  2668. //    SWSyncSpriteWorldToVBL with a value of false for your SpriteWorld before it calls
  2669. //    ExitToShell. You pass a pointer to the SpriteWorld you want to be "cleaned up".
  2670. ///--------------------------------------------------------------------------------------
  2671.  
  2672. SW_FUNC void SWSetCleanUpSpriteWorld(
  2673.     SpriteWorldPtr spriteWorldP)
  2674. {
  2675.     gSWCleanUpSpriteWorldP = spriteWorldP;
  2676. }
  2677.  
  2678.  
  2679. ///--------------------------------------------------------------------------------------
  2680. //    SWSetCleanUpFunction - installs a callback that is called whenever a fatal error or
  2681. //     assertion failure occurrs. Very useful for fading the screen back in, or doing any 
  2682. //    other important tasks that must be done before quitting when a fatal error occurrs. 
  2683. //    Your callback must be of this format:    void    MyCallBack(void).
  2684. ///--------------------------------------------------------------------------------------
  2685.  
  2686. SW_FUNC void SWSetCleanUpFunction(
  2687.     CleanUpCallBackPtr callBackP)
  2688. {
  2689.     gSWCleanUpCallBackP = callBackP;
  2690. }
  2691.  
  2692.  
  2693. ///--------------------------------------------------------------------------------------
  2694. //    SWGetSpriteWorldVersion - use the following in the 5th digit from the left:
  2695. //
  2696. //    developStage        = 0x20,
  2697. //    alphaStage            = 0x40,
  2698. //    betaStage            = 0x60,
  2699. //    finalStage            = 0x80
  2700. ///--------------------------------------------------------------------------------------
  2701.  
  2702. SW_FUNC unsigned long SWGetSpriteWorldVersion(void)
  2703. {
  2704.     return 0x02318000;        // i.e. 0x02306003 would be "2.3 beta 3"
  2705. }
  2706.